Update UserDto and delete addVeganMealProperty
This commit is contained in:
@@ -14,7 +14,6 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
@RunWith(MockitoJUnitRunner.class)
|
||||||
public class UserServiceTest {
|
public class UserServiceTest {
|
||||||
//logger that logs everything to the console
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
|
private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
@@ -26,8 +25,7 @@ public class UserServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void userRegisterTest() throws Exception {
|
public void userRegisterTest() throws Exception {
|
||||||
//tests if registering works
|
UserDto testUser = new UserDto(1L, "Eric");
|
||||||
UserDto testUser = new UserDto(1L, "Eric", 0);
|
|
||||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
|
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
|
||||||
UserDto.class))
|
UserDto.class))
|
||||||
.thenReturn(testUser);
|
.thenReturn(testUser);
|
||||||
@@ -38,25 +36,13 @@ public class UserServiceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void userLoginTest() throws Exception {
|
public void userLoginTest() throws Exception {
|
||||||
//tests if logging in works
|
UserDto testUser = new UserDto(1L, "Eric");
|
||||||
UserDto testUser = new UserDto(1L, "Eric", 0);
|
|
||||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
|
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
|
||||||
UserDto.class))
|
UserDto.class))
|
||||||
.thenReturn(testUser);
|
.thenReturn(testUser);
|
||||||
UserDto user = userService.loginUser("Eric", "password");
|
UserDto user = userService.loginUser("Eric", "password");
|
||||||
Assert.assertEquals(testUser, user);
|
Assert.assertEquals(testUser, user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void addVeganMealTest() throws Exception {
|
|
||||||
//tests if adding a vegetarian meal works
|
|
||||||
UserDto testUser = new UserDto(1L, "Eric", 0);
|
|
||||||
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/addVeganMeal?id=1&name=Eric"),
|
|
||||||
UserDto.class))
|
|
||||||
.thenReturn(testUser);
|
|
||||||
UserDto user = userService.addVeganMeal(1L, "Eric");
|
|
||||||
Assert.assertEquals(testUser, user);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
package greenify.server.data.model;
|
package greenify.server.data.model;
|
||||||
|
|
||||||
|
import greenify.server.InputValidator;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import javax.persistence.ElementCollection;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
@@ -25,7 +30,11 @@ public class User {
|
|||||||
@NotNull
|
@NotNull
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
private int veganMeal;
|
@NotNull
|
||||||
|
private Float footPrint = 0.0f;
|
||||||
|
|
||||||
|
@ElementCollection
|
||||||
|
private Map<String,String> footPrintInputs = new HashMap<>();
|
||||||
|
|
||||||
public User() {}
|
public User() {}
|
||||||
|
|
||||||
@@ -34,13 +43,12 @@ public class User {
|
|||||||
* @param id the id of the user.
|
* @param id the id of the user.
|
||||||
* @param name the supplied username
|
* @param name the supplied username
|
||||||
* @param password the supplied password
|
* @param password the supplied password
|
||||||
* @param veganMeal the supplied number of vegan meal
|
|
||||||
*/
|
*/
|
||||||
public User(Long id, String name, String password, int veganMeal) {
|
public User(Long id, String name, String password) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.veganMeal = veganMeal;
|
this.setFootPrintInputs(InputValidator.getDefaultValues());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -80,15 +88,23 @@ public class User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the number of vegan meal.
|
* gets the footPrint of user.
|
||||||
* @return the veganMeal
|
* @return the footPrint
|
||||||
*/
|
*/
|
||||||
public int getVeganMeal() {
|
public Float getFootPrint() {
|
||||||
return veganMeal;
|
return footPrint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVeganMeal(int veganMeal) {
|
public Map<String, String> getFootPrintInputs() {
|
||||||
this.veganMeal = veganMeal;
|
return footPrintInputs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFootPrintInputs(Map<String, String> footPrintInputs) {
|
||||||
|
this.footPrintInputs = footPrintInputs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFootPrint(Float footPrint) {
|
||||||
|
this.footPrint = footPrint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -99,7 +115,7 @@ public class User {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "User(id=" + this.id + ", name=" + this.name + ", password="
|
return "User(id=" + this.id + ", name=" + this.name + ", password="
|
||||||
+ this.password + ", veganMeal=" + this.veganMeal + ")";
|
+ this.password + ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -107,7 +123,7 @@ public class User {
|
|||||||
if (other instanceof User) {
|
if (other instanceof User) {
|
||||||
User that = (User)other;
|
User that = (User)other;
|
||||||
if (that.id == this.id && that.name.equals(this.name)
|
if (that.id == this.id && that.name.equals(this.name)
|
||||||
&& that.password.equals(this.password) && that.veganMeal == this.veganMeal) {
|
&& that.password.equals(this.password)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -116,6 +132,6 @@ public class User {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return Objects.hash(id, name, password, veganMeal);
|
return Objects.hash(id, name, password);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,49 +1,33 @@
|
|||||||
package greenify.server.rest;
|
package greenify.server.rest;
|
||||||
|
|
||||||
import greenify.common.UserDto;
|
import greenify.common.UserDto;
|
||||||
import greenify.server.data.model.User;
|
|
||||||
import greenify.server.data.repository.UserRepository;
|
|
||||||
import greenify.server.service.UserService;
|
import greenify.server.service.UserService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
//class that controls the user with regards to the server and sending data between them
|
|
||||||
//this class kind of 'redirects' the requests from the client to the server
|
|
||||||
@RestController
|
@RestController
|
||||||
public class UserController {
|
public class UserController {
|
||||||
@Autowired
|
@Autowired
|
||||||
UserService userService;
|
UserService userService;
|
||||||
|
|
||||||
//registers a user in the userService
|
|
||||||
@RequestMapping("/registerUser")
|
@RequestMapping("/registerUser")
|
||||||
//requestMapping is for the communication (GET, POST, PUT requests)
|
|
||||||
//as with Web and Database Technology
|
|
||||||
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) {
|
||||||
//the requestParams are the parameters that are sent with the request
|
|
||||||
//so in this case that it wants to register with the name and password
|
|
||||||
return userService.registerUser(name, password);
|
return userService.registerUser(name, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
//logs a user in in the userService
|
|
||||||
@RequestMapping("/loginUser")
|
@RequestMapping("/loginUser")
|
||||||
public UserDto loginUser(@RequestParam(value = "name") String name,
|
public UserDto loginUser(@RequestParam(value = "name") String name,
|
||||||
@RequestParam(value = "password") String password) {
|
@RequestParam(value = "password") String password) {
|
||||||
return userService.loginUser(name, password);
|
return userService.loginUser(name, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/setInput")
|
||||||
|
public void setInput(@RequestParam(value = "name") String name,
|
||||||
/**
|
@RequestParam(value = "inputName") String inputName,
|
||||||
* adds a vegetarian meal to the user.
|
@RequestParam(value = "value") String value) {
|
||||||
* @param id the id of the user
|
userService.setInput(name, inputName, value);
|
||||||
* @param name thr username of the user
|
|
||||||
*/
|
|
||||||
@RequestMapping("/addVeganMeal")
|
|
||||||
public void addVeganMeal(@RequestParam(value = "id") Long id,
|
|
||||||
@RequestParam(value = "name") String name) {
|
|
||||||
//here the requestParams are the id and name, because that is needed for the
|
|
||||||
//addVeganMeal method of the userService
|
|
||||||
userService.addVeganMeal(id, name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@ package greenify.server.service;
|
|||||||
|
|
||||||
import greenify.common.ApplicationException;
|
import greenify.common.ApplicationException;
|
||||||
import greenify.common.UserDto;
|
import greenify.common.UserDto;
|
||||||
|
import greenify.server.InputValidator;
|
||||||
import greenify.server.data.model.User;
|
import greenify.server.data.model.User;
|
||||||
import greenify.server.data.repository.UserRepository;
|
import greenify.server.data.repository.UserRepository;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -11,14 +12,15 @@ import org.springframework.stereotype.Service;
|
|||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
//userService class that gets used by the server to handle requests for users
|
|
||||||
@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;
|
||||||
//userRepository to talk with the database
|
|
||||||
|
@Autowired
|
||||||
|
CalculatorService calculatorService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* registers the user.
|
* registers the user.
|
||||||
@@ -28,17 +30,15 @@ public class UserService {
|
|||||||
*/
|
*/
|
||||||
public UserDto registerUser(String name, String password) {
|
public UserDto registerUser(String name, String password) {
|
||||||
User user = userRepository.findByName(name);
|
User user = userRepository.findByName(name);
|
||||||
//find the name of the user in the database
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
user = new User(null, name, password, 0);
|
user = new User(null, name, password);
|
||||||
//if the user isn't already in the database, save it in there
|
user.setFootPrintInputs(InputValidator.getDefaultValues());
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
} else {
|
} else {
|
||||||
throw new ApplicationException("User already exists");
|
throw new ApplicationException("User already exists");
|
||||||
}
|
}
|
||||||
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
|
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
|
||||||
//return a transferable user object that has been saved
|
return new UserDto(user.getId(), user.getName());
|
||||||
return new UserDto(user.getId(), user.getName(), user.getVeganMeal());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,34 +49,56 @@ public class UserService {
|
|||||||
*/
|
*/
|
||||||
public UserDto loginUser(String name, String password) {
|
public UserDto loginUser(String name, String password) {
|
||||||
User user = userRepository.findByName(name);
|
User user = userRepository.findByName(name);
|
||||||
//again find the name
|
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw new ApplicationException("User does not exist");
|
throw new ApplicationException("User does not exist");
|
||||||
//if it doesn't exist or the password is wrong, throw an exception
|
|
||||||
} else {
|
} else {
|
||||||
if (!user.getPassword().equals(password)) {
|
if (!user.getPassword().equals(password)) {
|
||||||
throw new ApplicationException("Wrong password");
|
throw new ApplicationException("Wrong password");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//return a transferable user object that has been logged in
|
return new UserDto(user.getId(), user.getName());
|
||||||
return new UserDto(user.getId(), user.getName(), user.getVeganMeal());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* add vegan meal to the user.
|
* The method sets input value.
|
||||||
* @param id the id of the user
|
* @param name of the user
|
||||||
* @param name the name of the user
|
* @param inputName is the name of the setting input
|
||||||
|
* @param value of the input
|
||||||
*/
|
*/
|
||||||
public void addVeganMeal(Long id, String name) {
|
public void setInput(String name, String inputName, String value) {
|
||||||
User user = userRepository.findByName(name);
|
User user = userRepository.findByName(name);
|
||||||
int count = user.getVeganMeal();
|
if (user == null) {
|
||||||
//find the user and update their vegetarian meal count
|
throw new ApplicationException("User does not exist");
|
||||||
count++;
|
} else {
|
||||||
user.setVeganMeal(count);
|
if (InputValidator.isValidItem(inputName)
|
||||||
//save it to the database
|
&& InputValidator.isValidItemValue(inputName, value)) {
|
||||||
userRepository.save(user);
|
user.getFootPrintInputs().put(inputName, value);
|
||||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
user.setFootPrint(calculatorService.calculateFootprint(user));
|
||||||
+ ", name=" + user.getName() + ")");
|
} else {
|
||||||
|
throw new ApplicationException("Invalid input");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the input value of an input.
|
||||||
|
* @param name of the user
|
||||||
|
* @param inputName name of the input
|
||||||
|
* @return input value
|
||||||
|
*/
|
||||||
|
public String getInput(String name, String inputName) {
|
||||||
|
User user = userRepository.findByName(name);
|
||||||
|
if (InputValidator.isValidItem(inputName)) {
|
||||||
|
String item = user.getFootPrintInputs().get(inputName);
|
||||||
|
return item;
|
||||||
|
} else {
|
||||||
|
throw new ApplicationException("Invalid input");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Float getFootprint(String name) {
|
||||||
|
User user = userRepository.findByName(name);
|
||||||
|
return calculatorService.calculateFootprint(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(path = "/all")
|
@GetMapping(path = "/all")
|
||||||
|
|||||||
@@ -13,36 +13,54 @@ public class UserTest {
|
|||||||
testUser.setId(1L);
|
testUser.setId(1L);
|
||||||
testUser.setName("greenify");
|
testUser.setName("greenify");
|
||||||
testUser.setPassword("password");
|
testUser.setPassword("password");
|
||||||
testUser.setVeganMeal(3);
|
User user = new User(1L, "greenify", "password");
|
||||||
User user = new User(1L, "greenify", "password", 3);
|
|
||||||
assertTrue(user.getId().equals(1L));
|
assertTrue(user.getId().equals(1L));
|
||||||
assertEquals(user.getName(), "greenify");
|
assertEquals(user.getName(), "greenify");
|
||||||
assertEquals(user.getPassword(), "password");
|
assertEquals(user.getPassword(), "password");
|
||||||
assertEquals(user.getVeganMeal(), 3);
|
|
||||||
assertEquals(user, testUser);
|
assertEquals(user, testUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toStringTest() {
|
public void toStringTest() {
|
||||||
User user = new User(1L, "greenify", "password", 3);
|
User user = new User(1L, "greenify", "password");
|
||||||
assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString());
|
assertEquals("User(id=1, name=greenify, password=password)", user.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void equalsTest() {
|
public void equalsTest() {
|
||||||
User first = new User(1L, "greenify", "password", 3);
|
User first = new User(1L, "greenify", "password");
|
||||||
User second = new User(1L, "greenify", "password", 3);
|
User second = new User(1L, "greenify", "password");
|
||||||
assertEquals(first.getId(), second.getId());
|
assertEquals(first.getId(), second.getId());
|
||||||
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());
|
|
||||||
assertTrue(first.equals(second));
|
assertTrue(first.equals(second));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equalsDifferentId() {
|
||||||
|
User first = new User(1L, "greenify", "password");
|
||||||
|
User second = new User(2L, "greenify", "password");
|
||||||
|
assertFalse(first.equals(second));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equalsDifferentName() {
|
||||||
|
User first = new User(1L, "greenify", "password");
|
||||||
|
User second = new User(1L, "hello", "password");
|
||||||
|
assertFalse(first.equals(second));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void equalsDifferentPassword() {
|
||||||
|
User first = new User(1L, "greenify", "password");
|
||||||
|
User second = new User(1L, "greenify", "hi");
|
||||||
|
assertFalse(first.equals(second));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void notEqualsTest() {
|
public void notEqualsTest() {
|
||||||
User first = new User(1L, "greenify", "password", 3);
|
User first = new User(1L, "greenify", "password");
|
||||||
User second = new User(1L, "greenify", "password", 7);
|
User second = new User(2L, "greenify", "password");
|
||||||
assertFalse(first.equals(second));
|
assertFalse(first.equals(second));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,8 +73,8 @@ public class UserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void hashCodeTest() {
|
public void hashCodeTest() {
|
||||||
User first = new User(1L, "greenify", "password", 3);
|
User first = new User(1L, "greenify", "password");
|
||||||
User second = new User(1L, "greenify", "password", 3);
|
User second = new User(1L, "greenify", "password");
|
||||||
assertEquals(first, second);
|
assertEquals(first, second);
|
||||||
assertEquals(first.hashCode(), second.hashCode());
|
assertEquals(first.hashCode(), second.hashCode());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ public class UserRepositoryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void findByUsernameTest() throws Exception {
|
public void findByUsernameTest() throws Exception {
|
||||||
repository.save(new User(296L, "cugurlu", "password", 6));
|
repository.save(new User(296L, "cugurlu", "password"));
|
||||||
User user = this.repository.findByName("cugurlu");
|
User user = this.repository.findByName("cugurlu");
|
||||||
assertEquals(user.getName(), "cugurlu");
|
assertEquals(user.getName(), "cugurlu");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ public class UserControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void registerUserTest() throws Exception {
|
public void registerUserTest() throws Exception {
|
||||||
given(this.userService.registerUser("name", "password"))
|
given(this.userService.registerUser("name", "password"))
|
||||||
.willReturn(new UserDto(1L, "name", 0));
|
.willReturn(new UserDto(1L, "name"));
|
||||||
mvc.perform(get("/registerUser")
|
mvc.perform(get("/registerUser")
|
||||||
.param("name", "name")
|
.param("name", "name")
|
||||||
.param("password", "password")
|
.param("password", "password")
|
||||||
@@ -46,7 +46,7 @@ public class UserControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void loginUserTest() throws Exception {
|
public void loginUserTest() throws Exception {
|
||||||
given(this.userService.loginUser("ceren", "password"))
|
given(this.userService.loginUser("ceren", "password"))
|
||||||
.willReturn(new UserDto(1L, "ceren", 0));
|
.willReturn(new UserDto(1L, "ceren"));
|
||||||
mvc.perform(get("/loginUser")
|
mvc.perform(get("/loginUser")
|
||||||
.param("name", "ceren")
|
.param("name", "ceren")
|
||||||
.param("password", "password")
|
.param("password", "password")
|
||||||
@@ -54,4 +54,9 @@ public class UserControllerTest {
|
|||||||
.andDo(print())
|
.andDo(print())
|
||||||
.andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}"));
|
.andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setInputTest() throws Exception {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package greenify.server.service;
|
package greenify.server.service;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
@@ -11,7 +12,6 @@ import greenify.server.data.repository.UserRepository;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.TestConfiguration;
|
import org.springframework.boot.test.context.TestConfiguration;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
@@ -34,12 +34,15 @@ public class UserServiceTest {
|
|||||||
@MockBean
|
@MockBean
|
||||||
private UserRepository userRepository;
|
private UserRepository userRepository;
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private CalculatorService calculatorService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* setUp method for test.
|
* setUp method for test.
|
||||||
*/
|
*/
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
User alex = new User(1L, "alex", "password", 0);
|
User alex = new User(1L, "alex", "password");
|
||||||
when(userRepository.findByName(alex.getName()))
|
when(userRepository.findByName(alex.getName()))
|
||||||
.thenReturn(alex);
|
.thenReturn(alex);
|
||||||
}
|
}
|
||||||
@@ -52,13 +55,96 @@ public class UserServiceTest {
|
|||||||
assertEquals(found.getName(), name);
|
assertEquals(found.getName(), name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void loginExceptionTest() {
|
||||||
|
assertThrows(ApplicationException.class, () -> {
|
||||||
|
userService.loginUser("alex", "greenify");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void userRegisterTest() {
|
public void userRegisterTest() {
|
||||||
User user = new User(1L, "name", "password", 0);
|
User user = new User(1L, "name", "password");
|
||||||
UserDto registered = userService.registerUser(user.getName(), user.getPassword());
|
UserDto registered = userService.registerUser(user.getName(), user.getPassword());
|
||||||
assertEquals(registered.getName(), "name");
|
assertEquals(registered.getName(), "name");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void registerExceptionTest() {
|
||||||
|
assertThrows(ApplicationException.class, () -> {
|
||||||
|
userService.registerUser("alex", "password");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setInputTest() {
|
||||||
|
User alex = new User(1L, "alex", "password");
|
||||||
|
when(userRepository.findByName(alex.getName()))
|
||||||
|
.thenReturn(alex);
|
||||||
|
userService.setInput("alex", "food_grains", "6.5");
|
||||||
|
assertEquals("6.5", alex.getFootPrintInputs().get("food_grains"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setInputNullTest() {
|
||||||
|
assertThrows(ApplicationException.class, () -> {
|
||||||
|
userService.setInput(null, "hello", "5.5");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setInputApplicationTestItem() {
|
||||||
|
assertThrows(ApplicationException.class, () -> {
|
||||||
|
userService.setInput("alex", "hello", "3.5");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setInputApplicationTestValue() {
|
||||||
|
assertThrows(ApplicationException.class, () -> {
|
||||||
|
userService.setInput("alex", "transportation_num_vehicles", "5.5");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setInputFootprintTest() {
|
||||||
|
User alex = new User(1L, "alex", "password");
|
||||||
|
when(userRepository.findByName(alex.getName()))
|
||||||
|
.thenReturn(alex);
|
||||||
|
when(calculatorService.calculateFootprint(alex))
|
||||||
|
.thenReturn(15f);
|
||||||
|
userService.setInput("alex", "food_grains", "6.5");
|
||||||
|
assertTrue(15f == alex.getFootPrint());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getInputTest() {
|
||||||
|
User alex = new User(1L, "alex", "password");
|
||||||
|
when(userRepository.findByName(alex.getName()))
|
||||||
|
.thenReturn(alex);
|
||||||
|
userService.setInput("alex", "food_grains", "6.5");
|
||||||
|
assertEquals("6.5", userService.getInput("alex", "food_grains"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getInputExceptionTest() {
|
||||||
|
assertThrows(ApplicationException.class, () -> {
|
||||||
|
userService.getInput("alex", "hello");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getFootprintTest() {
|
||||||
|
User alex = new User(1L, "alex", "password");
|
||||||
|
when(userRepository.findByName(alex.getName()))
|
||||||
|
.thenReturn(alex);
|
||||||
|
when(calculatorService.calculateFootprint(alex))
|
||||||
|
.thenReturn(15f);
|
||||||
|
userService.setInput("alex", "food_grains", "6.5");
|
||||||
|
assertTrue(15f == userService.getFootprint("alex"));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getAllUserTest() {
|
public void getAllUserTest() {
|
||||||
assertEquals(userRepository.findAll(), userService.getAllUsers());
|
assertEquals(userRepository.findAll(), userService.getAllUsers());
|
||||||
|
|||||||
Reference in New Issue
Block a user