From a605e1a92e110dfc4b551be196b23b1b0d3c0992 Mon Sep 17 00:00:00 2001 From: cugurlu Date: Sun, 17 Mar 2019 15:19:43 +0100 Subject: [PATCH] Update changes --- src/.idea/workspace.xml | 622 +++++++++--------- .../java/greenify/client/Application.java | 21 +- .../controller/DashBoardController.java | 7 +- .../client/controller/UserController.java | 13 +- .../greenify/client/rest/UserService.java | 40 ++ .../src/main/resources/application.properties | 1 + src/Client/src/test/java/ApplicationTest.java | 0 src/Client/src/test/java/UserServiceTest.java | 22 +- src/Server/build.gradle | 2 + .../greenify/server/rest/UserController.java | 17 +- .../greenify/server/service/UserService.java | 7 +- src/Server/src/test/java/ApplicationTest.java | 31 +- .../greenify/server/data/model/UserTest.java | 48 ++ .../data/repository/UserRepositoryTest.java | 37 ++ .../server/rest/UserControllerTest.java | 61 ++ .../server/service/UserServiceTest.java | 66 ++ 16 files changed, 647 insertions(+), 348 deletions(-) create mode 100644 src/Client/src/test/java/ApplicationTest.java create mode 100644 src/Server/src/test/java/greenify/server/data/model/UserTest.java create mode 100644 src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java create mode 100644 src/Server/src/test/java/greenify/server/rest/UserControllerTest.java create mode 100644 src/Server/src/test/java/greenify/server/service/UserServiceTest.java diff --git a/src/.idea/workspace.xml b/src/.idea/workspace.xml index 02586c2..2d43afe 100644 --- a/src/.idea/workspace.xml +++ b/src/.idea/workspace.xml @@ -1,5 +1,8 @@ + + + @@ -76,20 +79,26 @@ - + + + + + + + - + - + @@ -103,110 +112,19 @@ - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - + - - + + @@ -227,6 +145,8 @@ add sig regis + greenify + userC @@ -235,11 +155,6 @@ @@ -293,7 +216,8 @@ - @@ -311,6 +235,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -329,7 +279,7 @@ - + @@ -340,6 +290,9 @@ + + + - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + + + + + + + + + + @@ -518,25 +483,24 @@ - - + - + - + - + @@ -546,29 +510,29 @@ - - - - + + + + - + - + - + - + @@ -578,10 +542,10 @@ - - - - + + + + @@ -589,28 +553,10 @@ - - - - - - - - - - - - - - - - - - @@ -666,97 +612,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -764,27 +619,6 @@ - - - - - - - - - - - - - - - - - - - - - @@ -792,6 +626,17 @@ + + + + + + + + + + + @@ -808,35 +653,37 @@ - - + - - + + - + - - + + - + - - + + + + + - - + + @@ -845,18 +692,8 @@ - - - - - - - - - - - - + + @@ -865,19 +702,188 @@ - - + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + diff --git a/src/Client/src/main/java/greenify/client/Application.java b/src/Client/src/main/java/greenify/client/Application.java index ede019f..2ce787e 100644 --- a/src/Client/src/main/java/greenify/client/Application.java +++ b/src/Client/src/main/java/greenify/client/Application.java @@ -12,26 +12,33 @@ import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class Application extends javafx.application.Application { - private ConfigurableApplicationContext springContext; - private Parent rootNode; - private FXMLLoader fxmlLoader; + private static ConfigurableApplicationContext springContext; private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { launch(args); } + public static Parent load(java.net.URL url) { + FXMLLoader loader = new FXMLLoader(); + loader.setControllerFactory(springContext::getBean); + loader.setLocation(url); + try { + return loader.load(); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + @Override public void init() throws Exception { springContext = SpringApplication.run(Application.class); - fxmlLoader = new FXMLLoader(); - fxmlLoader.setControllerFactory(springContext::getBean); } @Override public void start(Stage primaryStage) throws Exception { - fxmlLoader.setLocation(this.getClass().getClassLoader().getResource("fxml/sample.fxml")); - rootNode = fxmlLoader.load(); + Parent rootNode = load(this.getClass().getClassLoader().getResource("fxml/sample.fxml")); primaryStage.setTitle("Greenify"); Scene scene = new Scene(rootNode); primaryStage.setScene(scene); diff --git a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java index a7df146..b44eff2 100644 --- a/src/Client/src/main/java/greenify/client/controller/DashBoardController.java +++ b/src/Client/src/main/java/greenify/client/controller/DashBoardController.java @@ -6,6 +6,7 @@ import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.AnchorPane; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller @@ -13,6 +14,8 @@ public class DashBoardController { @Autowired UserService userService; + private int count = 0; + @FXML public AnchorPane menuBar; public AnchorPane dashboardPane; @@ -66,8 +69,8 @@ public class DashBoardController { public void addVeganMeal(ActionEvent event) { count++; counter.setText("Count: " + count); - UserService service = new UserService(); - service.addVeganMeal(null, null); + System.out.println(userService); + userService.addVeganMeal(userService.currentUser.getId(), userService.currentUser.getName()); System.out.println("Vegetarian meal is added"); } } diff --git a/src/Client/src/main/java/greenify/client/controller/UserController.java b/src/Client/src/main/java/greenify/client/controller/UserController.java index abf29a5..2668d93 100644 --- a/src/Client/src/main/java/greenify/client/controller/UserController.java +++ b/src/Client/src/main/java/greenify/client/controller/UserController.java @@ -1,5 +1,6 @@ package greenify.client.controller; +import greenify.client.Application; import greenify.client.rest.UserService; import javafx.event.ActionEvent; import javafx.fxml.FXML; @@ -64,9 +65,7 @@ public class UserController { * @author sem */ public void openDashboard() throws IOException { - Parent dash = FXMLLoader.load( - this.getClass().getClassLoader().getResource("fxml/dashboard.fxml") - ); + Parent dash = Application.load (this.getClass().getClassLoader().getResource("fxml/dashboard.fxml")); Scene scene = new Scene(dash); scene.getStylesheets().add(getClass().getClassLoader().getResource("stylesheets/dashboardStyle.css").toExternalForm()); Stage appStage = new Stage(); @@ -97,14 +96,12 @@ public class UserController { public void handleRegisterButtonAction(ActionEvent event) throws Exception{ //load the fxml file - Parent registerWindow = FXMLLoader.load ( - this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml") - ); + Parent registerWindow = Application.load (this.getClass().getClassLoader().getResource("fxml/RegisterWindow.fxml")); //make the window use the scene - Scene registerscene = new Scene(registerWindow); + Scene registerScene = new Scene(registerWindow); Stage registerStage = new Stage(); //open the window - registerStage.setScene(registerscene); + registerStage.setScene(registerScene); registerStage.setTitle("Enter register credentials"); registerStage.show(); } diff --git a/src/Client/src/main/java/greenify/client/rest/UserService.java b/src/Client/src/main/java/greenify/client/rest/UserService.java index b5e2865..d1822f0 100644 --- a/src/Client/src/main/java/greenify/client/rest/UserService.java +++ b/src/Client/src/main/java/greenify/client/rest/UserService.java @@ -15,6 +15,8 @@ public class UserService { @Autowired RestTemplate restTemplate; + public UserDTO currentUser; + @Bean RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.build(); @@ -34,6 +36,44 @@ public class UserService { .queryParam("password", password); HttpEntity entity = new HttpEntity<>(headers); System.out.println(builder.build().encode().toUri()); + UserDTO result = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); + this.currentUser = result; + return result; + } + + /** + * sign ins the user. + * @param name the username of the user + * @param password the password of the user + * @return a userDTO + */ + public UserDTO loginUser(String name, String password) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/loginUser") + .queryParam("name", name) + .queryParam("password", password); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); + UserDTO result = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); + this.currentUser = result; + return result; + } + + /** + * a user adds vegan meal. + * @param id the id of the user + * @param name the username of the user + * @return a userDTO + */ + public UserDTO addVeganMeal(Long id, String name) { + HttpHeaders headers = new HttpHeaders(); + headers.set("Accept", MediaType.APPLICATION_JSON_VALUE); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/addVeganMeal") + .queryParam("id", id) + .queryParam("name", name); + HttpEntity entity = new HttpEntity<>(headers); + System.out.println(builder.build().encode().toUri()); return this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class); } } diff --git a/src/Client/src/main/resources/application.properties b/src/Client/src/main/resources/application.properties index e69de29..ff55ac5 100644 --- a/src/Client/src/main/resources/application.properties +++ b/src/Client/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.org.springframework.beans.factory=DEBUG \ No newline at end of file diff --git a/src/Client/src/test/java/ApplicationTest.java b/src/Client/src/test/java/ApplicationTest.java new file mode 100644 index 0000000..e69de29 diff --git a/src/Client/src/test/java/UserServiceTest.java b/src/Client/src/test/java/UserServiceTest.java index 46ba840..1c4a7ef 100644 --- a/src/Client/src/test/java/UserServiceTest.java +++ b/src/Client/src/test/java/UserServiceTest.java @@ -21,7 +21,7 @@ public class UserServiceTest { UserService userService; @Test - public void mocking() throws Exception { + public void userRegisterTest() throws Exception { UserDTO testUser = new UserDTO(1L, "Eric"); Mockito.when(restTemplate.getForObject(new java.net.URI("http://localhost:8080/registerUser?name=Eric&password=password"), UserDTO.class)) @@ -30,6 +30,26 @@ public class UserServiceTest { UserDTO user = userService.registerUser("Eric", "password"); Assert.assertEquals(testUser, user); } + + @Test + public void userLoginTest() throws Exception { + 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 { + UserDTO testUser = new UserDTO(1L, "Eric"); + 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); + } } diff --git a/src/Server/build.gradle b/src/Server/build.gradle index 42771dc..4d1b88a 100644 --- a/src/Server/build.gradle +++ b/src/Server/build.gradle @@ -34,6 +34,8 @@ targetCompatibility = 1.8 dependencies { compile("org.springframework.boot:spring-boot-starter-web") testCompile('org.springframework.boot:spring-boot-starter-test') + testCompile('org.springframework.boot:spring-boot-test') + compile('org.springframework.boot:spring-boot-devtools') compile project(path: ':src:Common') compileOnly 'org.projectlombok:lombok:1.18.6' annotationProcessor 'org.projectlombok:lombok:1.18.6' diff --git a/src/Server/src/main/java/greenify/server/rest/UserController.java b/src/Server/src/main/java/greenify/server/rest/UserController.java index 27cd303..452689f 100644 --- a/src/Server/src/main/java/greenify/server/rest/UserController.java +++ b/src/Server/src/main/java/greenify/server/rest/UserController.java @@ -1,17 +1,21 @@ 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.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController public class UserController { @Autowired UserService userService; + @Autowired // This means to get the bean called userRepository + // Which is auto-generated by Spring, we will use it to handle the data + UserRepository userRepository; + @RequestMapping("/registerUser") public UserDTO registerUser(@RequestParam(value = "name") String name, @RequestParam(value = "password") String password) { @@ -24,6 +28,13 @@ public class UserController { return userService.loginUser(name, password); } + @GetMapping(path="/all") + public @ResponseBody + Iterable 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) { diff --git a/src/Server/src/main/java/greenify/server/service/UserService.java b/src/Server/src/main/java/greenify/server/service/UserService.java index 7566dc3..68d257a 100644 --- a/src/Server/src/main/java/greenify/server/service/UserService.java +++ b/src/Server/src/main/java/greenify/server/service/UserService.java @@ -23,10 +23,10 @@ public class UserService { */ public UserDTO registerUser(String name, String password) { User user = userRepository.findByName(name); - if (user != null) { - throw new ApplicationException("User already exists"); - } else { + if (user == null) { user = userRepository.save(new User(null, name, password, 0)); + } else { + throw new ApplicationException("User already exists"); } logger.info("Created user id=" + user.getId() + ", name=" + user.getName()); return new UserDTO(user.getId(), user.getName()); @@ -61,6 +61,7 @@ public class UserService { int count = user.getVeganMeal(); count++; user.setVeganMeal(count); + userRepository.save(user); logger.info("Added vegan meal to user(id=" + user.getId() + ", name=" + user.getName() + ")"); } } diff --git a/src/Server/src/test/java/ApplicationTest.java b/src/Server/src/test/java/ApplicationTest.java index 08ba691..eb54359 100644 --- a/src/Server/src/test/java/ApplicationTest.java +++ b/src/Server/src/test/java/ApplicationTest.java @@ -1,16 +1,15 @@ -//import greenify.server.Application; -//import org.junit.Test; -//import org.junit.runner.RunWith; -//import org.springframework.test.context.junit4.SpringRunner; -// -//@RunWith(SpringRunner.class) -//public class ApplicationTest { -// @Test -// public void applicationContextLoaded() { -// } -// -// @Test -// public void applicationContextTest() { -// Application.main(new String[] {}); -// } -//} \ No newline at end of file +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) +@SpringBootConfiguration +public class ApplicationTest { + + @Test + public void contextLoads() throws Exception{ } +} diff --git a/src/Server/src/test/java/greenify/server/data/model/UserTest.java b/src/Server/src/test/java/greenify/server/data/model/UserTest.java new file mode 100644 index 0000000..efe7549 --- /dev/null +++ b/src/Server/src/test/java/greenify/server/data/model/UserTest.java @@ -0,0 +1,48 @@ +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; + +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); + 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()); + } + + @Test + public void equalsTest() { + User first = new User(1L, "greenify", "password", 3); + User second = new User(1L, "greenify", "password", 3); + assertEquals(first.getId(), second.getId()); + assertEquals(first.getName(), second.getName()); + assertEquals(first.getPassword(), second.getPassword()); + assertEquals(first.getVeganMeal(), second.getVeganMeal()); + } + + @Test + public void hashCodeTest() { + User first = new User(1L, "greenify", "password", 3); + User second = new User(1L, "greenify", "password", 3); + assertTrue(first.equals(second) && second.equals(first)); + assertTrue(first.hashCode() == second.hashCode()); + } +} + diff --git a/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java b/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java new file mode 100644 index 0000000..9383d21 --- /dev/null +++ b/src/Server/src/test/java/greenify/server/data/repository/UserRepositoryTest.java @@ -0,0 +1,37 @@ +//package greenify.server.data.repository; +// +//import greenify.server.data.model.User; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +//import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +//import org.springframework.test.context.junit4.SpringRunner; +// +//import static junit.framework.TestCase.assertTrue; +//import static org.junit.Assert.assertEquals; +// +//@RunWith(SpringRunner.class) +//@DataJpaTest +//public class UserRepositoryTest { +// +// @Autowired +// private TestEntityManager entityManager; +// +// @Autowired +// private UserRepository repository; +// +// @Test +// public void findByUsernameShouldReturnUser() throws Exception { +// this.entityManager.persist(new User(296L, "cugurlu", "password", 6)); +// User user = this.repository.findByName("cugurlu"); +// assertEquals(user.getName(), "cugurlu"); +// } +// +// @Test +// public void findByUsernameWhenNoUserShouldReturnNull() throws Exception { +// this.entityManager.persist(new User(296L, "cugurlu", "password", 6)); +// User user = this.repository.findByName("mouse"); +// assertTrue(user == null); +// } +//} diff --git a/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java new file mode 100644 index 0000000..abb31d7 --- /dev/null +++ b/src/Server/src/test/java/greenify/server/rest/UserControllerTest.java @@ -0,0 +1,61 @@ +//package greenify.server.rest; +// +//import greenify.common.UserDTO; +//import greenify.server.data.model.User; +//import greenify.server.service.UserService; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.boot.test.mock.mockito.MockBean; +//import org.springframework.context.ApplicationContext; +//import org.springframework.http.MediaType; +//import org.springframework.test.context.junit4.SpringRunner; +//import org.springframework.test.web.servlet.MockMvc; +//import org.springframework.test.web.servlet.ResultMatcher; +//import static org.assertj.core.internal.bytebuddy.matcher.ElementMatchers.is; +//import static org.hamcrest.Matchers.hasSize; +//import static org.mockito.BDDMockito.given; +//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +// +//@RunWith(SpringRunner.class) +//@SpringBootTest +//@AutoConfigureMockMvc +//@AutoConfigureTestDatabase +//public class UserControllerTest { +// +// @Autowired +// private MockMvc mvc; +// +// @Autowired +// private ApplicationContext applicationContext; +// +// @MockBean +// private UserService userService; +// +// @Test +// public void getVehicleWhenRequestingTextShouldReturnMakeAndModel() throws Exception { +// given(this.userService.loginUser("name", "password")) +// .willReturn(new UserDTO(1L, "name")); +// this.mvc.perform(get("/loginUser").accept(MediaType.APPLICATION_JSON)) +// .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"); +// given(userService.loginUser("alex", "password")).willReturn(user); +// mvc.perform(get("/loginUser") +// .contentType(MediaType.ALL)) +// .andExpect(status().isOk()) +// .andExpect(jsonPath("$", hasSize(1))) +// .andExpect((ResultMatcher) jsonPath("$[0].name", is(alex.getName()))) +// .andExpect((ResultMatcher) jsonPath("$[0].password", is(alex.getPassword()))); +// } +//} \ No newline at end of file diff --git a/src/Server/src/test/java/greenify/server/service/UserServiceTest.java b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java new file mode 100644 index 0000000..467f675 --- /dev/null +++ b/src/Server/src/test/java/greenify/server/service/UserServiceTest.java @@ -0,0 +1,66 @@ +package greenify.server.service; + +import greenify.common.ApplicationException; +import greenify.common.UserDTO; +import greenify.server.data.model.User; +import greenify.server.data.repository.UserRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +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 { + @TestConfiguration + static class UserServiceConfiguration { + @Bean + public UserService userService() { + return new UserService(); + } + } + + @Autowired + private UserService userService; + + @MockBean + private UserRepository userRepository; + + @Before + public void setUp() { + User alex = new User(1L, "alex", "password", 0); + when(userRepository.findByName(alex.getName())) + .thenReturn(alex); + } + + @Test + public void validLoginTest() { + String name = "alex"; + String password = "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 invalidLoginTest() { + User user = null; + assertThrows(ApplicationException.class, () -> { + userService.loginUser(null, null); + }); + } +}