Update UserDto and delete addVeganMealProperty

This commit is contained in:
cugurlu
2019-03-25 13:07:03 +01:00
parent e79d050b12
commit e8108498ad
8 changed files with 211 additions and 94 deletions

View File

@@ -14,7 +14,6 @@ import org.springframework.web.client.RestTemplate;
@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {
//logger that logs everything to the console
private static Logger logger = LoggerFactory.getLogger(UserServiceTest.class);
@Mock
@@ -26,8 +25,7 @@ public class UserServiceTest {
@Test
public void userRegisterTest() throws Exception {
//tests if registering works
UserDto testUser = new UserDto(1L, "Eric", 0);
UserDto testUser = new UserDto(1L, "Eric");
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"),
UserDto.class))
.thenReturn(testUser);
@@ -38,25 +36,13 @@ public class UserServiceTest {
@Test
public void userLoginTest() throws Exception {
//tests if logging in works
UserDto testUser = new UserDto(1L, "Eric", 0);
UserDto testUser = new UserDto(1L, "Eric");
Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/loginUser?name=Eric&password=password"),
UserDto.class))
.thenReturn(testUser);
UserDto user = userService.loginUser("Eric", "password");
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);
}
}

View File

@@ -1,8 +1,13 @@
package greenify.server.data.model;
import greenify.server.InputValidator;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@@ -25,7 +30,11 @@ public class User {
@NotNull
private String password;
private int veganMeal;
@NotNull
private Float footPrint = 0.0f;
@ElementCollection
private Map<String,String> footPrintInputs = new HashMap<>();
public User() {}
@@ -34,13 +43,12 @@ public class User {
* @param id the id of the user.
* @param name the supplied username
* @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.name = name;
this.password = password;
this.veganMeal = veganMeal;
this.setFootPrintInputs(InputValidator.getDefaultValues());
}
/**
@@ -80,15 +88,23 @@ public class User {
}
/**
* gets the number of vegan meal.
* @return the veganMeal
* gets the footPrint of user.
* @return the footPrint
*/
public int getVeganMeal() {
return veganMeal;
public Float getFootPrint() {
return footPrint;
}
public void setVeganMeal(int veganMeal) {
this.veganMeal = veganMeal;
public Map<String, String> getFootPrintInputs() {
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
public String toString() {
return "User(id=" + this.id + ", name=" + this.name + ", password="
+ this.password + ", veganMeal=" + this.veganMeal + ")";
+ this.password + ")";
}
@Override
@@ -107,7 +123,7 @@ public class User {
if (other instanceof User) {
User that = (User)other;
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;
}
}
@@ -116,6 +132,6 @@ public class User {
@Override
public int hashCode() {
return Objects.hash(id, name, password, veganMeal);
return Objects.hash(id, name, password);
}
}

View File

@@ -1,49 +1,33 @@
package greenify.server.rest;
import greenify.common.UserDto;
import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository;
import greenify.server.service.UserService;
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
public class UserController {
@Autowired
UserService userService;
//registers a user in the userService
@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,
@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);
}
//logs a user in in the userService
@RequestMapping("/loginUser")
public UserDto loginUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
return userService.loginUser(name, password);
}
/**
* adds a vegetarian meal to the user.
* @param id the id of the user
* @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);
@RequestMapping("/setInput")
public void setInput(@RequestParam(value = "name") String name,
@RequestParam(value = "inputName") String inputName,
@RequestParam(value = "value") String value) {
userService.setInput(name, inputName, value);
}
}

View File

@@ -2,6 +2,7 @@ package greenify.server.service;
import greenify.common.ApplicationException;
import greenify.common.UserDto;
import greenify.server.InputValidator;
import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository;
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.ResponseBody;
//userService class that gets used by the server to handle requests for users
@Service
public class UserService {
Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
UserRepository userRepository;
//userRepository to talk with the database
@Autowired
CalculatorService calculatorService;
/**
* registers the user.
@@ -28,17 +30,15 @@ public class UserService {
*/
public UserDto registerUser(String name, String password) {
User user = userRepository.findByName(name);
//find the name of the user in the database
if (user == null) {
user = new User(null, name, password, 0);
//if the user isn't already in the database, save it in there
user = new User(null, name, password);
user.setFootPrintInputs(InputValidator.getDefaultValues());
userRepository.save(user);
} else {
throw new ApplicationException("User already exists");
}
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(), user.getVeganMeal());
return new UserDto(user.getId(), user.getName());
}
/**
@@ -49,34 +49,56 @@ public class UserService {
*/
public UserDto loginUser(String name, String password) {
User user = userRepository.findByName(name);
//again find the name
if (user == null) {
throw new ApplicationException("User does not exist");
//if it doesn't exist or the password is wrong, throw an exception
} else {
if (!user.getPassword().equals(password)) {
throw new ApplicationException("Wrong password");
}
}
//return a transferable user object that has been logged in
return new UserDto(user.getId(), user.getName(), user.getVeganMeal());
return new UserDto(user.getId(), user.getName());
}
/**
* add vegan meal to the user.
* @param id the id of the user
* @param name the name of the user
* The method sets input value.
* @param 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);
int count = user.getVeganMeal();
//find the user and update their vegetarian meal count
count++;
user.setVeganMeal(count);
//save it to the database
userRepository.save(user);
logger.info("Added vegan meal to user(id=" + user.getId()
+ ", name=" + user.getName() + ")");
if (user == null) {
throw new ApplicationException("User does not exist");
} else {
if (InputValidator.isValidItem(inputName)
&& InputValidator.isValidItemValue(inputName, value)) {
user.getFootPrintInputs().put(inputName, value);
user.setFootPrint(calculatorService.calculateFootprint(user));
} 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")

View File

@@ -13,36 +13,54 @@ public class UserTest {
testUser.setId(1L);
testUser.setName("greenify");
testUser.setPassword("password");
testUser.setVeganMeal(3);
User user = new User(1L, "greenify", "password", 3);
User user = new User(1L, "greenify", "password");
assertTrue(user.getId().equals(1L));
assertEquals(user.getName(), "greenify");
assertEquals(user.getPassword(), "password");
assertEquals(user.getVeganMeal(), 3);
assertEquals(user, testUser);
}
@Test
public void toStringTest() {
User user = new User(1L, "greenify", "password", 3);
assertEquals("User(id=1, name=greenify, password=password, veganMeal=3)", user.toString());
User user = new User(1L, "greenify", "password");
assertEquals("User(id=1, name=greenify, password=password)", user.toString());
}
@Test
public void equalsTest() {
User first = new User(1L, "greenify", "password", 3);
User second = new User(1L, "greenify", "password", 3);
User first = new User(1L, "greenify", "password");
User second = new User(1L, "greenify", "password");
assertEquals(first.getId(), second.getId());
assertEquals(first.getName(), second.getName());
assertEquals(first.getPassword(), second.getPassword());
assertEquals(first.getVeganMeal(), second.getVeganMeal());
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
public void notEqualsTest() {
User first = new User(1L, "greenify", "password", 3);
User second = new User(1L, "greenify", "password", 7);
User first = new User(1L, "greenify", "password");
User second = new User(2L, "greenify", "password");
assertFalse(first.equals(second));
}
@@ -55,8 +73,8 @@ public class UserTest {
@Test
public void hashCodeTest() {
User first = new User(1L, "greenify", "password", 3);
User second = new User(1L, "greenify", "password", 3);
User first = new User(1L, "greenify", "password");
User second = new User(1L, "greenify", "password");
assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
}

View File

@@ -17,7 +17,7 @@ public class UserRepositoryTest {
@Test
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");
assertEquals(user.getName(), "cugurlu");
}

View File

@@ -34,7 +34,7 @@ public class UserControllerTest {
@Test
public void registerUserTest() throws Exception {
given(this.userService.registerUser("name", "password"))
.willReturn(new UserDto(1L, "name", 0));
.willReturn(new UserDto(1L, "name"));
mvc.perform(get("/registerUser")
.param("name", "name")
.param("password", "password")
@@ -46,7 +46,7 @@ public class UserControllerTest {
@Test
public void loginUserTest() throws Exception {
given(this.userService.loginUser("ceren", "password"))
.willReturn(new UserDto(1L, "ceren", 0));
.willReturn(new UserDto(1L, "ceren"));
mvc.perform(get("/loginUser")
.param("name", "ceren")
.param("password", "password")
@@ -54,4 +54,9 @@ public class UserControllerTest {
.andDo(print())
.andExpect(status().isOk()).andExpect(content().json("{'id':1,'name':'ceren'}"));
}
@Test
public void setInputTest() throws Exception {
}
}

View File

@@ -1,6 +1,7 @@
package greenify.server.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
@@ -11,7 +12,6 @@ import greenify.server.data.repository.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -34,12 +34,15 @@ public class UserServiceTest {
@MockBean
private UserRepository userRepository;
@MockBean
private CalculatorService calculatorService;
/**
* setUp method for test.
*/
@Before
public void setUp() {
User alex = new User(1L, "alex", "password", 0);
User alex = new User(1L, "alex", "password");
when(userRepository.findByName(alex.getName()))
.thenReturn(alex);
}
@@ -52,13 +55,96 @@ public class UserServiceTest {
assertEquals(found.getName(), name);
}
@Test
public void loginExceptionTest() {
assertThrows(ApplicationException.class, () -> {
userService.loginUser("alex", "greenify");
});
}
@Test
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());
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
public void getAllUserTest() {
assertEquals(userRepository.findAll(), userService.getAllUsers());