FIX:: checkstyle 'server', things with user

This commit is contained in:
mlwauben
2019-03-26 09:49:27 +01:00
committed by Merel Steenbergen
parent 61e31df157
commit 71ea1e3a4b
8 changed files with 136 additions and 94 deletions

View File

@@ -34,7 +34,7 @@ public class User {
private Float footPrint = 0.0f;
@ElementCollection
private Map<String,String> footPrintInputs = new HashMap<>();
private Map<String, String> footPrintInputs = new HashMap<>();
@ManyToMany
@JoinColumn
@@ -43,7 +43,7 @@ public class User {
public User() {}
/**
* makes a user object.
* This method makes a user object.
* @param id the id of the user.
* @param name the supplied username
* @param password the supplied password
@@ -57,53 +57,81 @@ public class User {
}
/**
* gets the id.
* @return the id
* This method returns the ID of the user.
* @return the id of the user
*/
public Long getId() {
return id;
}
public void setId(Long id) {
/**
* This method sets the ID of the user.
* @param id the id of the user
*/
void setId(Long id) {
this.id = id;
}
/**
* gets the name.
* @return the name
* This method returns the name of the user.
* @return the name of the user
*/
public String getName() {
return name;
}
/**
* This method sets the name of the user.
* @param name the name of the user
*/
public void setName(String name) {
this.name = name;
}
/**
* gets the password.
* @return the password
* This method returns the password of the user.
* @return the password of the user
*/
public String getPassword() {
return password;
}
public void setPassword(String password) {
/**
* This method sets the password of the user.
* @param password the password of the user
*/
void setPassword(String password) {
this.password = password;
}
/**
* gets the footPrint of user.
* @return the footPrint
* This method returns the footPrint of user.
* @return the footprint of the user
*/
public Float getFootPrint() {
return footPrint;
}
/**
* This method sets the footprint of a user.
* @param footPrint footprint of a user
*/
public void setFootPrint(Float footPrint) {
this.footPrint = footPrint;
}
/**
* This method returns the footprint inputs of the user.
* @return footprint inputs of the user
*/
public Map<String, String> getFootPrintInputs() {
return footPrintInputs;
}
/**
* This method sets the footprint inputs of the user.
* @param footPrintInputs footprint inputs of the user
*/
public void setFootPrintInputs(Map<String, String> footPrintInputs) {
this.footPrintInputs = footPrintInputs;
}
@@ -126,13 +154,8 @@ public class User {
}
}
public void setFootPrint(Float footPrint) {
this.footPrint = footPrint;
}
/**
* Returns a human readable object. It's in JSON.
* This method returns a human readable (JSON) object.
* @return the JSON form of the object.
*/
@Override
@@ -142,6 +165,7 @@ public class User {
}
/**
<<<<<<< HEAD
* Returns the name and score of the friends in JSON. Needed for the leaderboard.
* @return a JSON object of the friendlist with only names and scores.
*/
@@ -156,18 +180,24 @@ public class User {
return result + "]";
}
/** This method checks whether two users are equal or not.
* @param other an other user
* @return users are (not) equal
*/
@Override
public boolean equals(Object other) {
if (other instanceof User) {
User that = (User)other;
if (that.id == this.id && that.name.equals(this.name)
&& that.password.equals(this.password)) {
return true;
}
User that = (User) other;
return that.id.equals(this.id) && that.name.equals(this.name)
&& that.password.equals(this.password);
}
return false;
}
/**
* This method returns the hashcode of a user.
* @return hashcode of a user
*/
@Override
public int hashCode() {
return Objects.hash(id, name, password);

View File

@@ -3,10 +3,17 @@ package greenify.server.data.repository;
import greenify.server.data.model.User;
import org.springframework.data.repository.CrudRepository;
//userRepository that saves all the user and talks to the database
/**
* UserRepository that saves all the users and talks to the database.
*/
public interface UserRepository extends CrudRepository<User, Integer> {
User findByName(String name);
/**
* This method saves a user in the database.
* @param user the user you want saved
* @param <T> always a user
* @return the user
*/
<T extends User> T save(T user);
}

View File

@@ -10,24 +10,43 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private
UserService userService;
/**
* This method registers the user.
* @param name name of the user
* @param password password of the user
* @return the userDto of the user
*/
@RequestMapping("/registerUser")
public UserDto registerUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
return userService.registerUser(name, password);
}
/**
* This method logs in the user.
* @param name name of the user
* @param password password of the user
* @return the userDto of the user
*/
@RequestMapping("/loginUser")
public UserDto loginUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
return userService.loginUser(name, password);
}
/**
* This method sets input for a user.
* @param name name of the user
* @param inputName name of the input of the user
* @param value value of the input
*/
@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

@@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.ResponseBody;
@Service
public class UserService {
Logger logger = LoggerFactory.getLogger(UserService.class);
private Logger logger = LoggerFactory.getLogger(UserService.class);
@Autowired
UserRepository userRepository;
@@ -23,10 +23,10 @@ public class UserService {
CalculatorService calculatorService;
/**
* registers the user.
* @param name the username of the user
* @param password the password of the user
* @return a userDTO of the registered user
* This method registers the user.
* @param name name of the user
* @param password password of the user
* @return the userDto of the user
*/
public UserDto registerUser(String name, String password) {
User user = userRepository.findByName(name);
@@ -42,10 +42,10 @@ public class UserService {
}
/**
* logs the user in.
* @param name the username of the user
* @param password the password of the user
* @return a userDTO of the logged in user
* This method logs in the user.
* @param name name of the user
* @param password password of the user
* @return the userDto of the user
*/
public UserDto loginUser(String name, String password) {
User user = userRepository.findByName(name);
@@ -60,6 +60,7 @@ public class UserService {
}
/**
<<<<<<< HEAD
* Adds a friend to the friendlist of the user.
* @param name the username of the user
* @param friend the name of the friend you want to add.
@@ -85,10 +86,10 @@ public class UserService {
}
/**
* The method sets input value.
* @param name of the user
* @param inputName is the name of the setting input
* @param value of the input
* This method sets input for a user.
* @param name name of the user
* @param inputName name of the input of the user
* @param value value of the input
*/
public void setInput(String name, String inputName, String value) {
User user = userRepository.findByName(name);
@@ -106,31 +107,37 @@ public class UserService {
}
/**
* Gets the input value of an input.
* This method returns 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) {
String getInput(String name, String inputName) {
User user = userRepository.findByName(name);
if (InputValidator.isValidItem(inputName)) {
String item = user.getFootPrintInputs().get(inputName);
return item;
return user.getFootPrintInputs().get(inputName);
} else {
throw new ApplicationException("Invalid input");
}
}
public Float getFootprint(String name) {
/**
* This method returns the footprint of a user.
* @param name name of the user
* @return footprint of the user
*/
Float getFootprint(String name) {
User user = userRepository.findByName(name);
return calculatorService.calculateFootprint(user);
}
/**
* This method returns a JSON of XML with all users.
* @return JSON/XML of all users
*/
@GetMapping(path = "/all")
@ResponseBody
public Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
Iterable<User> getAllUsers() {
return userRepository.findAll();
}
}

View File

@@ -1,16 +1,15 @@
package greenify.server.data.model;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import greenify.common.ApplicationException;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.ArrayList;
public class UserTest {
@Test
public void setAndGetTest() {
User testUser = new User();
@@ -18,7 +17,7 @@ public class UserTest {
testUser.setName("greenify");
testUser.setPassword("password");
User user = new User(1L, "greenify", "password");
assertTrue(user.getId().equals(1L));
assertEquals(1L, (long) user.getId());
assertEquals(user.getName(), "greenify");
assertEquals(user.getPassword(), "password");
assertEquals(user, testUser);
@@ -32,47 +31,47 @@ public class UserTest {
@Test
public void equalsTest() {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "greenify", "password");
User first = new User(2L, "greenify", "12345");
User second = new User(2L, "greenify", "12345");
assertEquals(first.getId(), second.getId());
assertEquals(first.getName(), second.getName());
assertEquals(first.getPassword(), second.getPassword());
assertTrue(first.equals(second));
assertEquals(first, second);
}
@Test
public void equalsDifferentId() {
User first = new User(1L, "greenify", "password");
User second = new User(2L, "greenify", "password");
assertFalse(first.equals(second));
assertNotEquals(first, second);
}
@Test
public void equalsDifferentName() {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "hello", "password");
assertFalse(first.equals(second));
User first = new User(5L, "greenify", "password");
User second = new User(5L, "hello", "password");
assertNotEquals(first, second);
}
@Test
public void equalsDifferentPassword() {
User first = new User(1L, "greenify", "password");
User second = new User(1L, "greenify", "hi");
assertFalse(first.equals(second));
assertNotEquals(first, second);
}
@Test
public void notEqualsTest() {
User first = new User(1L, "greenify", "password");
User second = new User(2L, "greenify", "password");
assertFalse(first.equals(second));
assertNotEquals(first, second);
}
@Test
public void instanceOfTest() {
User first = new User();
Object second = new Object();
assertFalse(first.equals(second));
assertNotEquals(first, second);
}
@Test
@@ -120,4 +119,3 @@ public class UserTest {
assertEquals(first.friendsToString(), "friends=[{name=merel, footprint=0.0}]");
}
}

View File

@@ -1,7 +1,6 @@
package greenify.server.data.repository;
import static org.junit.Assert.assertEquals;
import greenify.server.data.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -16,9 +15,10 @@ public class UserRepositoryTest {
private UserRepository repository;
@Test
public void findByUsernameTest() throws Exception {
public void findByUsernameTest() {
repository.save(new User(296L, "cugurlu", "password"));
User user = this.repository.findByName("cugurlu");
assertEquals(user.getName(), "cugurlu");
}
}

View File

@@ -56,7 +56,6 @@ public class UserControllerTest {
}
@Test
public void setInputTest() throws Exception {
}
public void setInputTest() {
}
}

View File

@@ -1,7 +1,6 @@
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;
@@ -62,9 +61,7 @@ public class UserServiceTest {
@Test
public void loginExceptionTest() {
assertThrows(ApplicationException.class, () -> {
userService.loginUser("alex", "greenify");
});
assertThrows(ApplicationException.class, () -> userService.loginUser("alex", "greenify"));
}
@Test
@@ -76,9 +73,7 @@ public class UserServiceTest {
@Test
public void registerExceptionTest() {
assertThrows(ApplicationException.class, () -> {
userService.registerUser("alex", "password");
});
assertThrows(ApplicationException.class, () -> userService.registerUser("alex", "password"));
}
@Test
@@ -92,26 +87,19 @@ public class UserServiceTest {
@Test
public void setInputNullTest() {
assertThrows(ApplicationException.class, () -> {
userService.setInput(null, "hello", "5.5");
});
assertThrows(ApplicationException.class, () -> userService.setInput(null, "hello", "5.5"));
}
@Test
public void setInputApplicationTestItem() {
assertThrows(ApplicationException.class, () -> {
userService.setInput("alex", "hello", "3.5");
});
assertThrows(ApplicationException.class, () -> userService.setInput("tom", "hello", "3.5"));
}
@Test
public void setInputApplicationTestValue() {
assertThrows(ApplicationException.class, () -> {
userService.setInput("alex", "transportation_num_vehicles", "5.5");
});
assertThrows(ApplicationException.class, () -> userService.setInput("tom", "transportation_num_vehicles", "5.5"));
}
@Test
public void setInputFootprintTest() {
User alex = new User(1L, "alex", "password");
@@ -120,7 +108,7 @@ public class UserServiceTest {
when(calculatorService.calculateFootprint(alex))
.thenReturn(15f);
userService.setInput("alex", "food_grains", "6.5");
assertTrue(15f == alex.getFootPrint());
assertEquals(15f, alex.getFootPrint(), 0.0);
}
@Test
@@ -134,9 +122,7 @@ public class UserServiceTest {
@Test
public void getInputExceptionTest() {
assertThrows(ApplicationException.class, () -> {
userService.getInput("alex", "hello");
});
assertThrows(ApplicationException.class, () -> userService.getInput("alex", "hello"));
}
@Test
@@ -147,7 +133,7 @@ public class UserServiceTest {
when(calculatorService.calculateFootprint(alex))
.thenReturn(15f);
userService.setInput("alex", "food_grains", "6.5");
assertTrue(15f == userService.getFootprint("alex"));
assertEquals(15f, userService.getFootprint("alex"), 0.0);
}
@Test
@@ -157,10 +143,7 @@ public class UserServiceTest {
@Test
public void invalidLoginTest() {
User user = null;
assertThrows(ApplicationException.class, () -> {
userService.loginUser(null, null);
});
assertThrows(ApplicationException.class, () -> userService.loginUser(null, null));
}
@Test
@@ -182,5 +165,4 @@ public class UserServiceTest {
assertEquals(userService.getLeaderboard("alex"), "friends=[{name=lola, footprint=0.0}]");
}
}