RoleControllerTest

This commit is contained in:
lukasgol committed 2015-01-19 21:32:02 +01:00
1 parent 20313caa16
commit 891d4bfa4c
4 files changed
+69 -18

No files matched your search

@@ -19,13 +19,19 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/roles")
public class RoleController {
public void setRoleRepository(RoleRepository roleRepository) {
this.roleRepository = roleRepository;
}
@Autowired
private RoleRepository roleRepository;
@RequestMapping(value = "/add")
public void addRole(@RequestParam String rolename,@RequestParam String password) {
public Role addRole(@RequestParam String rolename,@RequestParam String password) {
Role role = new Role(rolename,password);
roleRepository.save(role);
Role result = roleRepository.save(role);
return result;
}
@RequestMapping(value = "/addSampleRoles")
+13 -1
View File
@@ -9,6 +9,10 @@ import java.util.Set;
*/
@Entity (name = "role")
public class Role {
public void setRoleId(long roleId) {
this.roleId = roleId;
}
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.AUTO)
@@ -63,5 +67,13 @@ public class Role {
return true;
}
@Override
public String toString() {
return "Role{" +
"roleId=" + roleId +
", roleName='" + roleName + '\'' +
", password='" + password + '\'' +
", users=" + users +
'}';
}
}
+41 -15
View File
@@ -1,8 +1,11 @@
package test;
import application.controllers.DoorController;
import application.controllers.RoleController;
import application.model.Door;
import application.model.Role;
import application.repositories.DoorRepository;
import application.repositories.RoleRepository;
import org.json.JSONException;
import org.junit.Test;
import org.springframework.ui.ModelMap;
@@ -20,21 +23,44 @@ import static org.mockito.Mockito.when;
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());
public void addRoleTest(){
RoleController roleController=new RoleController();
String rolename = "aaa";
String password = "bbb";
Role role = new Role(rolename,password);
role.setRoleId(0);
RoleRepository roleRepository= mock(RoleRepository.class);
when(roleRepository.save(role)).thenReturn(role);
roleController.setRoleRepository(roleRepository);
Role result = roleController.addRole(rolename, password);
assertEquals(role.toString(),result.toString());
}
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 getRoleTest() throws JSONException {
RoleController roleController=new RoleController();
String rolename = "aaa";
String password = "bbb";
String roleString ="[{\"password\":\""+password+"\",\"name\":\""+rolename+"\"}]";
long id=0;
Role role = new Role(rolename,password);
RoleRepository roleRepository= mock(RoleRepository.class);
when(roleRepository.findOne(id)).thenReturn(role);
roleController.setRoleRepository(roleRepository);
String result = roleController.getRole(id);
assertEquals(roleString,result);
}
@Test
public void removeRoleTest() throws JSONException {
RoleController roleController=new RoleController();
String rolename = "aaa";
String password = "bbb";
long id=0;
Role role = new Role(rolename,password);
RoleRepository roleRepository= mock(RoleRepository.class);
roleController.setRoleRepository(roleRepository);
String result = roleController.removeRole(id);
assertEquals("role deleted: 0",result);
}
}
@@ -0,0 +1,7 @@
package test;
/**
* Created by lukasgol on 19.01.15.
*/
public class UserControllerTest {
}