DoorControllerTest
This commit is contained in:
1 parent
eb4796ca34
commit
20313caa16
4 files changed
+115
-6
No files matched your search
+5
-1
@@ -29,7 +29,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
compile("com.fasterxml.jackson.core:jackson-databind")
|
||||
testCompile("junit:junit")
|
||||
compile("junit:junit")
|
||||
compile 'org.springframework:spring-core:4.0.0.RELEASE'
|
||||
compile("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||
compile("com.h2database:h2")
|
||||
@@ -38,6 +38,10 @@ dependencies {
|
||||
compile("org.springframework.boot:spring-boot-starter-web")
|
||||
testCompile("org.springframework.boot:spring-boot-starter-test")
|
||||
providedRuntime("javax.servlet:jstl")
|
||||
compile 'com.google.code.gson:gson:2.2.4'
|
||||
compile "org.mockito:mockito-core:1.+"
|
||||
|
||||
|
||||
|
||||
/*Potrzebne do mapowanie widokow jsp, u*/
|
||||
// providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
|
||||
|
||||
@@ -20,6 +20,10 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
@Controller
|
||||
@RequestMapping(value = "/doors")
|
||||
public class DoorController {
|
||||
public void setDoorRepository(DoorRepository doorRepository) {
|
||||
this.doorRepository = doorRepository;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
DoorRepository doorRepository;
|
||||
|
||||
@@ -46,11 +50,12 @@ public class DoorController {
|
||||
loginResponse.put("success", true);
|
||||
loginResponse.put("doorId", door.getDoorId());
|
||||
JSONObject jsonmessage = new JSONObject();
|
||||
jsonmessage.put("dateTime",message.getDateTime() );
|
||||
jsonmessage.put("message",message.getMessage() );
|
||||
jsonmessage.put("serverMessageId",message.getServerMessageId() );
|
||||
loginResponse.put("serverMessage", jsonmessage);
|
||||
|
||||
if(message!=null) {
|
||||
jsonmessage.put("dateTime", message.getDateTime());
|
||||
jsonmessage.put("message", message.getMessage());
|
||||
jsonmessage.put("serverMessageId", message.getServerMessageId());
|
||||
loginResponse.put("serverMessage", jsonmessage);
|
||||
}
|
||||
return loginResponse.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package test;
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 19.01.15.
|
||||
*/
|
||||
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;
|
||||
|
||||
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(){
|
||||
long doorId=0;
|
||||
DoorController doorController=new DoorController();
|
||||
String expectedMessage="{\"success\":true}";
|
||||
Door door = new Door();
|
||||
|
||||
DoorRepository doorRepository= mock(DoorRepository.class);
|
||||
when(doorRepository.findOne(doorId)).thenReturn(door);
|
||||
String result=null;
|
||||
doorController.setDoorRepository(doorRepository);
|
||||
try {
|
||||
result= doorController.getUser(doorId);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
assertEquals( expectedMessage, result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package test;
|
||||
|
||||
import application.controllers.DoorController;
|
||||
import application.model.Door;
|
||||
import application.repositories.DoorRepository;
|
||||
import org.json.JSONException;
|
||||
import org.junit.Test;
|
||||
import org.springframework.ui.ModelMap;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Created by lukasgol on 19.01.15.
|
||||
*/
|
||||
public class RoleControllerTest {
|
||||
|
||||
@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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user