完成用户接口编写,通过单元测试

This commit is contained in:
freeebird 2018-11-13 19:12:28 +08:00
parent 40d4e46f74
commit 7b3aadbc14
2 changed files with 119 additions and 0 deletions

View File

@ -1,4 +1,73 @@
package cn.mafangui.hotel.controller;
import cn.mafangui.hotel.entity.User;
import cn.mafangui.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(method = RequestMethod.POST,value = "/login")
public int userLogin(String username,String password){
int result = 0;
if (userService.selectByUsernameAndPassword(username,password) != null){
result = 1;
}
else result = 0;
return result;
}
@RequestMapping(method = RequestMethod.POST,value = "/register")
public int userRegister(String username,String password,String name,String gender,String phone,String email,String address,String idcard){
User user = new User(username,password,name,gender,phone,email,address,idcard);
return userService.insertUser(user);
}
@RequestMapping(method = RequestMethod.POST,value = "/update")
public int userUpdate(int userId,String name,String gender,String phone,String email,String address,String idcard){
User user = new User();
user.setUserId(userId);
user.setName(name);
user.setGender(gender);
user.setPhone(phone);
user.setEmail(email);
user.setAddress(address);
user.setIdcard(idcard);
return userService.updateUser(user);
}
@RequestMapping(method = RequestMethod.POST,value = "/updatePassword")
public int updatePassword(String username,String oldPassword,String newPassword){
User user = userService.selectByUsernameAndPassword(username,oldPassword);
if (user == null){
return -1;
}else {
user.setPassword(newPassword);
return userService.updateUser(user);
}
}
@RequestMapping(value = "/all")
public List<User> getAllUser(){
return userService.selectAll();
}
@RequestMapping(method = RequestMethod.POST,value = "/isUsernameExist")
public int isUsernameExist(String username){
int result = 0;
if (userService.selectByUsername(username) != null){
result = 1;
}
else result = 0;
return result;
}
}

View File

@ -0,0 +1,50 @@
package cn.mafangui.hotel.controller;
import cn.mafangui.hotel.entity.User;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserControllerTest {
@Autowired
UserController uc;
@Test
public void userLogin() {
Assert.assertEquals(1,uc.userLogin("nihao","123456"));
}
@Test
public void userRegister() {
int res = uc.userRegister("hi","hi","","","1123","132","ddd","123");
Assert.assertEquals(1,res);
}
@Test
public void userUpdate() {
Assert.assertEquals(1,uc.userUpdate(3,null,null,null,null,"add",null));
}
@Test
public void updatePassword() {
Assert.assertEquals(1,uc.updatePassword("hi","hi","123"));
}
@Test
public void getAllUser() {
Assert.assertEquals(2,uc.getAllUser().size());
}
@Test
public void isUsernameExist() {
Assert.assertEquals(0,uc.isUsernameExist("addd"));
}
}