FIX:: checkstyle 'server', things with user

This commit is contained in:
mlwauben
2019-03-26 09:49:27 +01:00
parent 0ae5e7f4ee
commit 72cb770861
8 changed files with 141 additions and 97 deletions

View File

@@ -36,10 +36,10 @@ public class User {
@ElementCollection @ElementCollection
private Map<String, String> footPrintInputs = new HashMap<>(); private Map<String, String> footPrintInputs = new HashMap<>();
public User() {} User() { }
/** /**
* makes a user object. * This method makes a user object.
* @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
@@ -52,64 +52,87 @@ public class User {
} }
/** /**
* gets the id. * This method returns the ID of the user.
* @return the id * @return the id of the user
*/ */
public Long getId() { public Long getId() {
return id; 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; this.id = id;
} }
/** /**
* gets the name. * This method returns the name of the user.
* @return the name * @return the name of the user
*/ */
public String getName() { public String getName() {
return name; return name;
} }
/**
* This method sets the name of the user.
* @param name the name of the user
*/
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
/** /**
* gets the password. * This method returns the password of the user.
* @return the password * @return the password of the user
*/ */
public String getPassword() { public String getPassword() {
return password; 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; this.password = password;
} }
/** /**
* gets the footPrint of user. * This method returns the footPrint of user.
* @return the footPrint * @return the footprint of the user
*/ */
public Float getFootPrint() { public Float getFootPrint() {
return footPrint; return footPrint;
} }
public Map<String, String> getFootPrintInputs() { /**
return footPrintInputs; * This method sets the footprint of a user.
} * @param footPrint footprint of a user
*/
public void setFootPrintInputs(Map<String, String> footPrintInputs) {
this.footPrintInputs = footPrintInputs;
}
public void setFootPrint(Float footPrint) { public void setFootPrint(Float footPrint) {
this.footPrint = 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;
}
/** /**
* Returns a human readable object. It's in JSON. * 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;
}
/**
* This method returns a human readable (JSON) object.
* @return the JSON form of the object. * @return the JSON form of the object.
*/ */
@Override @Override
@@ -118,18 +141,25 @@ public class User {
+ this.password + ")"; + this.password + ")";
} }
/**
* This method checks whether two users are equal or not.
* @param other an other user
* @return users are (not) equal
*/
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
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) return that.id.equals(this.id) && that.name.equals(this.name)
&& that.password.equals(this.password)) { && that.password.equals(this.password);
return true;
}
} }
return false; return false;
} }
/**
* This method returns the hashcode of a user.
* @return hashcode of a user
*/
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(id, name, password); return Objects.hash(id, name, password);

View File

@@ -3,10 +3,17 @@ package greenify.server.data.repository;
import greenify.server.data.model.User; import greenify.server.data.model.User;
import org.springframework.data.repository.CrudRepository; 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> { public interface UserRepository extends CrudRepository<User, Integer> {
User findByName(String name); 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); <T extends User> T save(T user);
} }

View File

@@ -10,20 +10,39 @@ import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class UserController { public class UserController {
@Autowired @Autowired
private
UserService userService; 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") @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) {
return userService.registerUser(name, 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") @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);
} }
/**
* 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") @RequestMapping("/setInput")
public void setInput(@RequestParam(value = "name") String name, public void setInput(@RequestParam(value = "name") String name,
@RequestParam(value = "inputName") String inputName, @RequestParam(value = "inputName") String inputName,

View File

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

View File

@@ -1,12 +1,10 @@
package greenify.server.data.model; 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; import org.junit.Test;
import static org.junit.Assert.*;
public class UserTest { public class UserTest {
@Test @Test
public void setAndGetTest() { public void setAndGetTest() {
User testUser = new User(); User testUser = new User();
@@ -14,7 +12,7 @@ public class UserTest {
testUser.setName("greenify"); testUser.setName("greenify");
testUser.setPassword("password"); testUser.setPassword("password");
User user = new User(1L, "greenify", "password"); User user = new User(1L, "greenify", "password");
assertTrue(user.getId().equals(1L)); assertEquals(1L, (long) user.getId());
assertEquals(user.getName(), "greenify"); assertEquals(user.getName(), "greenify");
assertEquals(user.getPassword(), "password"); assertEquals(user.getPassword(), "password");
assertEquals(user, testUser); assertEquals(user, testUser);
@@ -28,47 +26,47 @@ public class UserTest {
@Test @Test
public void equalsTest() { public void equalsTest() {
User first = new User(1L, "greenify", "password"); User first = new User(2L, "greenify", "12345");
User second = new User(1L, "greenify", "password"); User second = new User(2L, "greenify", "12345");
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());
assertTrue(first.equals(second)); assertEquals(first, second);
} }
@Test @Test
public void equalsDifferentId() { public void equalsDifferentId() {
User first = new User(1L, "greenify", "password"); User first = new User(1L, "greenify", "password");
User second = new User(2L, "greenify", "password"); User second = new User(2L, "greenify", "password");
assertFalse(first.equals(second)); assertNotEquals(first, second);
} }
@Test @Test
public void equalsDifferentName() { public void equalsDifferentName() {
User first = new User(1L, "greenify", "password"); User first = new User(5L, "greenify", "password");
User second = new User(1L, "hello", "password"); User second = new User(5L, "hello", "password");
assertFalse(first.equals(second)); assertNotEquals(first, second);
} }
@Test @Test
public void equalsDifferentPassword() { public void equalsDifferentPassword() {
User first = new User(1L, "greenify", "password"); User first = new User(1L, "greenify", "password");
User second = new User(1L, "greenify", "hi"); User second = new User(1L, "greenify", "hi");
assertFalse(first.equals(second)); assertNotEquals(first, second);
} }
@Test @Test
public void notEqualsTest() { public void notEqualsTest() {
User first = new User(1L, "greenify", "password"); User first = new User(1L, "greenify", "password");
User second = new User(2L, "greenify", "password"); User second = new User(2L, "greenify", "password");
assertFalse(first.equals(second)); assertNotEquals(first, second);
} }
@Test @Test
public void instanceOfTest() { public void instanceOfTest() {
User first = new User(); User first = new User();
Object second = new Object(); Object second = new Object();
assertFalse(first.equals(second)); assertNotEquals(first, second);
} }
@Test @Test
@@ -78,5 +76,5 @@ public class UserTest {
assertEquals(first, second); assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode()); assertEquals(first.hashCode(), second.hashCode());
} }
}
}

View File

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

View File

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

View File

@@ -1,7 +1,6 @@
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;
@@ -57,9 +56,7 @@ public class UserServiceTest {
@Test @Test
public void loginExceptionTest() { public void loginExceptionTest() {
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> userService.loginUser("alex", "greenify"));
userService.loginUser("alex", "greenify");
});
} }
@Test @Test
@@ -71,9 +68,7 @@ public class UserServiceTest {
@Test @Test
public void registerExceptionTest() { public void registerExceptionTest() {
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> userService.registerUser("alex", "password"));
userService.registerUser("alex", "password");
});
} }
@Test @Test
@@ -87,23 +82,17 @@ public class UserServiceTest {
@Test @Test
public void setInputNullTest() { public void setInputNullTest() {
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> userService.setInput(null, "hello", "5.5"));
userService.setInput(null, "hello", "5.5");
});
} }
@Test @Test
public void setInputApplicationTestItem() { public void setInputApplicationTestItem() {
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> userService.setInput("tom", "hello", "3.5"));
userService.setInput("alex", "hello", "3.5");
});
} }
@Test @Test
public void setInputApplicationTestValue() { public void setInputApplicationTestValue() {
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> userService.setInput("tom", "transportation_num_vehicles", "5.5"));
userService.setInput("alex", "transportation_num_vehicles", "5.5");
});
} }
@@ -115,7 +104,7 @@ public class UserServiceTest {
when(calculatorService.calculateFootprint(alex)) when(calculatorService.calculateFootprint(alex))
.thenReturn(15f); .thenReturn(15f);
userService.setInput("alex", "food_grains", "6.5"); userService.setInput("alex", "food_grains", "6.5");
assertTrue(15f == alex.getFootPrint()); assertEquals(15f, alex.getFootPrint(), 0.0);
} }
@Test @Test
@@ -129,9 +118,7 @@ public class UserServiceTest {
@Test @Test
public void getInputExceptionTest() { public void getInputExceptionTest() {
assertThrows(ApplicationException.class, () -> { assertThrows(ApplicationException.class, () -> userService.getInput("alex", "hello"));
userService.getInput("alex", "hello");
});
} }
@Test @Test
@@ -142,7 +129,7 @@ public class UserServiceTest {
when(calculatorService.calculateFootprint(alex)) when(calculatorService.calculateFootprint(alex))
.thenReturn(15f); .thenReturn(15f);
userService.setInput("alex", "food_grains", "6.5"); userService.setInput("alex", "food_grains", "6.5");
assertTrue(15f == userService.getFootprint("alex")); assertEquals(15f, userService.getFootprint("alex"), 0.0);
} }
@Test @Test
@@ -152,9 +139,7 @@ public class UserServiceTest {
@Test @Test
public void invalidLoginTest() { public void invalidLoginTest() {
User user = null; assertThrows(ApplicationException.class, () -> userService.loginUser(null, null));
assertThrows(ApplicationException.class, () -> {
userService.loginUser(null, null);
});
} }
} }