Add tests
This commit is contained in:
@@ -0,0 +1,12 @@
|
|||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.boot.SpringBootConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootConfiguration
|
||||||
|
public class ApplicationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void contextLoads() throws Exception{ }
|
||||||
|
}
|
||||||
|
|||||||
@@ -131,4 +131,4 @@ public class Activity {
|
|||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, name, description, score);
|
return Objects.hash(id, name, description, score);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,4 +91,21 @@ public class User {
|
|||||||
public void setVeganMeal(int veganMeal) {
|
public void setVeganMeal(int veganMeal) {
|
||||||
this.veganMeal = veganMeal;
|
this.veganMeal = veganMeal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if(other instanceof User){
|
||||||
|
User that = (User)other;
|
||||||
|
if(that.id != this.id)
|
||||||
|
return false;
|
||||||
|
if(!that.name.equals(this.name))
|
||||||
|
return false;
|
||||||
|
if(!that.password.equals(this.password))
|
||||||
|
return false;
|
||||||
|
if(that.veganMeal != this.veganMeal)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,10 +16,6 @@ public class UserController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserService userService;
|
UserService userService;
|
||||||
|
|
||||||
@Autowired // This means to get the bean called userRepository
|
|
||||||
// Which is auto-generated by Spring, we will use it to handle the data
|
|
||||||
UserRepository userRepository;
|
|
||||||
|
|
||||||
@RequestMapping("/registerUser")
|
@RequestMapping("/registerUser")
|
||||||
public UserDto registerUser(@RequestParam(value = "name") String name,
|
public UserDto registerUser(@RequestParam(value = "name") String name,
|
||||||
@RequestParam(value = "password") String password) {
|
@RequestParam(value = "password") String password) {
|
||||||
@@ -37,11 +33,4 @@ public class UserController {
|
|||||||
@RequestParam(value = "name") String name) {
|
@RequestParam(value = "name") String name) {
|
||||||
userService.addVeganMeal(id, name);
|
userService.addVeganMeal(id, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "/all")
|
|
||||||
@ResponseBody
|
|
||||||
public Iterable<User> getAllUsers() {
|
|
||||||
// This returns a JSON or XML with the users
|
|
||||||
return userRepository.findAll();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -8,10 +8,13 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
@@ -64,5 +67,12 @@ public class UserService {
|
|||||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
logger.info("Added vegan meal to user(id=" + user.getId()
|
||||||
+ ", name=" + user.getName() + ")");
|
+ ", name=" + user.getName() + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping(path = "/all")
|
||||||
|
@ResponseBody
|
||||||
|
public Iterable<User> getAllUsers() {
|
||||||
|
// This returns a JSON or XML with the users
|
||||||
|
return userRepository.findAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +1,19 @@
|
|||||||
import greenify.server.data.model.Activity;
|
package greenify.server.data.model;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class ActivityTest {
|
public class ActivityTest {
|
||||||
@Test
|
@Test
|
||||||
public void setAndGetTest() {
|
public void setAndGetTest() {
|
||||||
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
|
|
||||||
Activity testActivity = new Activity(0, null, null, 0);
|
Activity testActivity = new Activity(0, null, null, 0);
|
||||||
testActivity.setId(1L);
|
testActivity.setId(1L);
|
||||||
testActivity.setName("Vegetarian");
|
testActivity.setName("Vegetarian");
|
||||||
testActivity.setDescription("Eating");
|
testActivity.setDescription("Eating");
|
||||||
testActivity.setScore(100);
|
testActivity.setScore(100);
|
||||||
|
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
|
||||||
assertTrue(activity.getId().equals(1L));
|
assertTrue(activity.getId().equals(1L));
|
||||||
assertEquals(activity.getName(), "Vegetarian");
|
assertEquals(activity.getName(), "Vegetarian");
|
||||||
assertEquals(activity.getDescription(), "Eating");
|
assertEquals(activity.getDescription(), "Eating");
|
||||||
@@ -23,7 +24,8 @@ public class ActivityTest {
|
|||||||
@Test
|
@Test
|
||||||
public void toStringTest() {
|
public void toStringTest() {
|
||||||
Activity activity = new Activity(1, "Solar panels", "Installed", 10000);
|
Activity activity = new Activity(1, "Solar panels", "Installed", 10000);
|
||||||
assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString());
|
assertEquals("Activity(id=1, name=Solar panels, "
|
||||||
|
+ "description=Installed, score=10000)", activity.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -43,6 +45,5 @@ public class ActivityTest {
|
|||||||
Activity second = new Activity(1, "Solar panels", "Installed", 10000);
|
Activity second = new Activity(1, "Solar panels", "Installed", 10000);
|
||||||
assertEquals(first, second);
|
assertEquals(first, second);
|
||||||
assertEquals(first.hashCode(), second.hashCode());
|
assertEquals(first.hashCode(), second.hashCode());
|
||||||
// assertTrue(first.hashCode() == second.hashCode());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package greenify.server.data.model;
|
package greenify.server.data.model;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -35,6 +36,14 @@ public class UserTest {
|
|||||||
assertEquals(first.getName(), second.getName());
|
assertEquals(first.getName(), second.getName());
|
||||||
assertEquals(first.getPassword(), second.getPassword());
|
assertEquals(first.getPassword(), second.getPassword());
|
||||||
assertEquals(first.getVeganMeal(), second.getVeganMeal());
|
assertEquals(first.getVeganMeal(), second.getVeganMeal());
|
||||||
|
assertTrue(first.equals(second));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void instanceOfTest() {
|
||||||
|
User first = new User();
|
||||||
|
Object second = new Object();
|
||||||
|
assertFalse(first.equals(second));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -42,7 +51,7 @@ public class UserTest {
|
|||||||
User first = new User(1L, "greenify", "password", 3);
|
User first = new User(1L, "greenify", "password", 3);
|
||||||
User second = new User(1L, "greenify", "password", 3);
|
User second = new User(1L, "greenify", "password", 3);
|
||||||
assertTrue(first.equals(second) && second.equals(first));
|
assertTrue(first.equals(second) && second.equals(first));
|
||||||
assertTrue(first.hashCode() == second.hashCode());
|
assertEquals(first, second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,15 +51,30 @@ public class UserServiceTest {
|
|||||||
assertEquals(found.getName(), name);
|
assertEquals(found.getName(), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAllUserTest() {
|
||||||
|
assertEquals(userRepository.findAll(), userService.getAllUsers());
|
||||||
|
}
|
||||||
|
|
||||||
// @Test
|
// @Test
|
||||||
// public void addVeganMealTest() {
|
// public void validRegisterTest() {
|
||||||
// User user = new User(1L, "x", "y", 3);
|
// String name = "new";
|
||||||
// userRepository.save(user);
|
// String password = "user";
|
||||||
// System.out.println(userRepository);
|
// if(userRepository.findByName(name) == null) {
|
||||||
// userService.addVeganMeal(1L, "x");
|
// UserDto found = userService.registerUser(name, password);
|
||||||
// assertEquals(user.getVeganMeal(), 7);
|
// assertEquals(found.getName(), name);
|
||||||
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// @Test
|
||||||
|
// public void addVeganMealTest() {
|
||||||
|
// User user = new User(1L, "name", "password", 0);
|
||||||
|
// User user = userRepository.findByName("x");
|
||||||
|
// int veganMeal = user.getVeganMeal();
|
||||||
|
// userService.addVeganMeal(user.getId(), user.getName());
|
||||||
|
// assertEquals(user.getVeganMeal(), veganMeal++);
|
||||||
|
// }
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invalidLoginTest() {
|
public void invalidLoginTest() {
|
||||||
User user = null;
|
User user = null;
|
||||||
|
|||||||
Reference in New Issue
Block a user