Fix merge conflicts

This commit is contained in:
Merel Steenbergen
2019-03-17 17:54:24 +01:00
26 changed files with 815 additions and 670 deletions

View File

@@ -13,6 +13,7 @@ apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
apply plugin: 'checkstyle'
bootJar {
baseName = 'gs-rest-service'
@@ -31,6 +32,22 @@ repositories {
sourceCompatibility = 1.8
targetCompatibility = 1.8
def configDir = "${project.rootDir}/quality"
checkstyle {
toolVersion '7.8.1'
configFile file("$configDir/checkstyle/checkstyle.xml")
configProperties.checkstyleSuppressionsPath = file("$configDir/checkstyle/suppressions.xml").absolutePath
}
checkstyleMain {
source ='src/main/java'
}
checkstyleTest {
source ='src/test/java'
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile('org.springframework.boot:spring-boot-starter-test')

View File

@@ -50,7 +50,9 @@ public class User {
return id;
}
public void setId(Long id) { this.id = id; }
public void setId(Long id) {
this.id = id;
}
/**
* gets the name.
@@ -60,7 +62,9 @@ public class User {
return name;
}
public void setName(String name) { this.name = name; }
public void setName(String name) {
this.name = name;
}
/**
* gets the password.

View File

@@ -1,10 +1,11 @@
package greenify.server.data.repository;
import org.springframework.data.repository.CrudRepository;
import greenify.server.data.model.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Integer> {
User findByName(String name);
<T extends User> T save(T user);
}

View File

@@ -1,11 +1,15 @@
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@@ -17,27 +21,27 @@ public class UserController {
UserRepository userRepository;
@RequestMapping("/registerUser")
public UserDTO registerUser(@RequestParam(value = "name") String name,
public UserDto registerUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
return userService.registerUser(name, password);
}
@RequestMapping("/loginUser")
public UserDTO loginUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
public UserDto loginUser(@RequestParam(value = "name") String name,
@RequestParam(value = "password") String password) {
return userService.loginUser(name, password);
}
@GetMapping(path="/all")
public @ResponseBody
Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
@RequestMapping("/addVeganMeal")
public void addVeganMeal(@RequestParam(value = "id") Long id,
@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();
}
}

View File

@@ -1,7 +1,7 @@
package greenify.server.service;
import greenify.common.ApplicationException;
import greenify.common.UserDTO;
import greenify.common.UserDto;
import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository;
import org.slf4j.Logger;
@@ -21,7 +21,7 @@ public class UserService {
* @param password the password of the user
* @return a userDTO of the registered user
*/
public UserDTO registerUser(String name, String password) {
public UserDto registerUser(String name, String password) {
User user = userRepository.findByName(name);
if (user == null) {
user = userRepository.save(new User(null, name, password, 0));
@@ -29,7 +29,7 @@ public class UserService {
throw new ApplicationException("User already exists");
}
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
return new UserDTO(user.getId(), user.getName());
return new UserDto(user.getId(), user.getName());
}
/**
@@ -38,7 +38,7 @@ public class UserService {
* @param password the password of the user
* @return a userDTO of the logged in user
*/
public UserDTO loginUser(String name, String password) {
public UserDto loginUser(String name, String password) {
User user = userRepository.findByName(name);
if (user == null) {
throw new ApplicationException("User does not exist");
@@ -47,14 +47,13 @@ public class UserService {
throw new ApplicationException("Wrong password");
}
}
return new UserDTO(user.getId(), user.getName());
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
* @return a userDTO of the user added vegan meal
*/
public void addVeganMeal(Long id, String name) {
User user = userRepository.findByName(name);
@@ -62,7 +61,8 @@ public class UserService {
count++;
user.setVeganMeal(count);
userRepository.save(user);
logger.info("Added vegan meal to user(id=" + user.getId() + ", name=" + user.getName() + ")");
logger.info("Added vegan meal to user(id=" + user.getId()
+ ", name=" + user.getName() + ")");
}
}

View File

@@ -1,9 +1,6 @@
import greenify.server.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)

View File

@@ -1,19 +1,19 @@
package greenify.server.data.model;
import greenify.server.data.model.User;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
public class UserTest {
@Test
public void setAndGetTest() {
User user = new User(1L, "greenify", "password", 3);
User testUser = new User();
testUser.setId(1L);
testUser.setName("greenify");
testUser.setPassword("password");
testUser.setVeganMeal(3);
User user = new User(1L, "greenify", "password", 3);
assertTrue(user.getId().equals(1L));
assertEquals(user.getName(), "greenify");
assertEquals(user.getPassword(), "password");

View File

@@ -1,6 +1,6 @@
//package greenify.server.rest;
//
//import greenify.common.UserDTO;
//import greenify.common.UserDto;
//import greenify.server.data.model.User;
//import greenify.server.service.UserService;
//import org.junit.Test;
@@ -40,16 +40,18 @@
// @Test
// public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception {
// given(this.userService.loginUser("name", "password"))
// .willReturn(new UserDTO(1L, "name"));
// .willReturn(new UserDto(1L, "name"));
// this.mvc.perform(get("/loginUser").accept(MediaType.APPLICATION_JSON))
// .andExpect(status().isOk()).andExpect(content().json("name=name, password=password"));
// .andExpect(status().isOk())
// .andExpect(content()
// .json("name=name, password=password"));
// }
//
//
// @Test
// public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception {
// User alex = new User(1L, "alex", "password", 0);
// UserDTO user = userService.loginUser("alex", "password");
// UserDto user = userService.loginUser("alex", "password");
// given(userService.loginUser("alex", "password")).willReturn(user);
// mvc.perform(get("/loginUser")
// .contentType(MediaType.ALL))

View File

@@ -1,7 +1,11 @@
package greenify.server.service;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
import greenify.common.ApplicationException;
import greenify.common.UserDTO;
import greenify.common.UserDto;
import greenify.server.data.model.User;
import greenify.server.data.repository.UserRepository;
import org.junit.Before;
@@ -12,9 +16,6 @@ import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class UserServiceTest {
@@ -32,6 +33,9 @@ public class UserServiceTest {
@MockBean
private UserRepository userRepository;
/**
* setUp method for test.
*/
@Before
public void setUp() {
User alex = new User(1L, "alex", "password", 0);
@@ -43,18 +47,18 @@ public class UserServiceTest {
public void validLoginTest() {
String name = "alex";
String password = "password";
UserDTO found = userService.loginUser(name, password);
UserDto found = userService.loginUser(name, password);
assertEquals(found.getName(), name);
}
// @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);
// }
// @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);
// }
@Test
public void invalidLoginTest() {