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() {
|
||||
return Objects.hash(id, name, description, score);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,4 +91,21 @@ public class User {
|
||||
public void setVeganMeal(int 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
|
||||
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")
|
||||
public UserDto registerUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
@@ -37,11 +33,4 @@ public class UserController {
|
||||
@RequestParam(value = "name") String 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.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
@@ -64,5 +67,12 @@ public class UserService {
|
||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
||||
+ ", 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;
|
||||
import org.junit.Test;
|
||||
package greenify.server.data.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ActivityTest {
|
||||
@Test
|
||||
public void setAndGetTest() {
|
||||
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
|
||||
Activity testActivity = new Activity(0, null, null, 0);
|
||||
testActivity.setId(1L);
|
||||
testActivity.setName("Vegetarian");
|
||||
testActivity.setDescription("Eating");
|
||||
testActivity.setScore(100);
|
||||
Activity activity = new Activity(1L, "Vegetarian", "Eating", 100);
|
||||
assertTrue(activity.getId().equals(1L));
|
||||
assertEquals(activity.getName(), "Vegetarian");
|
||||
assertEquals(activity.getDescription(), "Eating");
|
||||
@@ -23,7 +24,8 @@ public class ActivityTest {
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
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
|
||||
@@ -43,6 +45,5 @@ public class ActivityTest {
|
||||
Activity second = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
assertEquals(first, second);
|
||||
assertEquals(first.hashCode(), second.hashCode());
|
||||
// assertTrue(first.hashCode() == second.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package greenify.server.data.model;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -35,6 +36,14 @@ public class UserTest {
|
||||
assertEquals(first.getName(), second.getName());
|
||||
assertEquals(first.getPassword(), second.getPassword());
|
||||
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
|
||||
@@ -42,7 +51,7 @@ public class UserTest {
|
||||
User first = new User(1L, "greenify", "password", 3);
|
||||
User second = new User(1L, "greenify", "password", 3);
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllUserTest() {
|
||||
assertEquals(userRepository.findAll(), userService.getAllUsers());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void addVeganMealTest() {
|
||||
// User user = new User(1L, "x", "y", 3);
|
||||
// userRepository.save(user);
|
||||
// System.out.println(userRepository);
|
||||
// userService.addVeganMeal(1L, "x");
|
||||
// assertEquals(user.getVeganMeal(), 7);
|
||||
// public void validRegisterTest() {
|
||||
// String name = "new";
|
||||
// String password = "user";
|
||||
// if(userRepository.findByName(name) == null) {
|
||||
// UserDto found = userService.registerUser(name, password);
|
||||
// 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
|
||||
public void invalidLoginTest() {
|
||||
User user = null;
|
||||
|
||||
Reference in New Issue
Block a user