Update changes

This commit is contained in:
cugurlu
2019-03-17 15:19:43 +01:00
parent 50c58da035
commit a605e1a92e
16 changed files with 647 additions and 348 deletions

View File

@@ -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'

View File

@@ -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<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) {

View File

@@ -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() + ")");
}
}

View File

@@ -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[] {});
// }
//}
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{ }
}

View File

@@ -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());
}
}

View File

@@ -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);
// }
//}

View File

@@ -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())));
// }
//}

View File

@@ -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);
});
}
}