Merge branch 'master' into 'AddFeatureClass'
# Conflicts: # src/Server/src/main/java/greenify/server/data/model/User.java # src/Server/src/main/java/greenify/server/service/UserService.java
This commit is contained in:
@@ -15,13 +15,6 @@ apply plugin: 'io.spring.dependency-management'
|
||||
apply plugin: 'jacoco'
|
||||
apply plugin: 'checkstyle'
|
||||
|
||||
|
||||
tasks.withType(Checkstyle) {
|
||||
reports {
|
||||
html.destination rootProject.file("build/reports/checkstyle.html")
|
||||
}
|
||||
}
|
||||
|
||||
bootJar {
|
||||
baseName = 'gs-rest-service'
|
||||
version = '0.1.0'
|
||||
@@ -39,14 +32,33 @@ 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')
|
||||
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'
|
||||
compile("org.springframework.boot:spring-boot-starter-data-jpa")
|
||||
compile("com.h2database:h2")
|
||||
compile("org.springframework.boot:spring-boot-starter-actuator")
|
||||
testCompile(
|
||||
'junit:junit:4.12',
|
||||
'org.junit.jupiter:junit-jupiter-api:5.4.0'
|
||||
@@ -55,6 +67,7 @@ dependencies {
|
||||
'org.junit.jupiter:junit-jupiter-engine:5.4.0',
|
||||
'org.junit.vintage:junit-vintage-engine:5.4.0'
|
||||
)
|
||||
compile 'javax.xml.bind:jaxb-api:2.3.0'
|
||||
}
|
||||
|
||||
jacoco {
|
||||
@@ -70,8 +83,3 @@ jacocoTestReport {
|
||||
}
|
||||
}
|
||||
|
||||
checkstyle {
|
||||
toolVersion = "7.6.1"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -49,7 +49,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.
|
||||
@@ -59,7 +61,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.
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package greenify.server.data.repository;
|
||||
|
||||
import greenify.server.data.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserJpaRepository extends UserRepository, JpaRepository<User,Long> {
|
||||
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package greenify.server.data.repository;
|
||||
|
||||
import greenify.server.data.model.User;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository {
|
||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||
User findByName(String name);
|
||||
|
||||
<T extends User> T save(T user);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package greenify.server.rest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
import greenify.server.data.model.User;
|
||||
import greenify.server.data.repository.UserRepository;
|
||||
|
||||
@Controller // This means that this class is a Controller
|
||||
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
|
||||
public class MainController {
|
||||
@Autowired // This means to get the bean called userRepository
|
||||
// Which is auto-generated by Spring, we will use it to handle the data
|
||||
private UserRepository userRepository;
|
||||
|
||||
@GetMapping(path="/add") // Map ONLY GET Requests
|
||||
public @ResponseBody String addNewUser (@RequestParam String name
|
||||
, @RequestParam String password) {
|
||||
// @ResponseBody means the returned String is the response, not a view name
|
||||
// @RequestParam means it is a parameter from the GET or POST request
|
||||
|
||||
User n = new User();
|
||||
n.setName(name);
|
||||
n.setPassword(password);
|
||||
userRepository.save(n);
|
||||
return "Saved";
|
||||
}
|
||||
|
||||
@GetMapping(path="/all")
|
||||
public @ResponseBody Iterable<User> getAllUsers() {
|
||||
// This returns a JSON or XML with the users
|
||||
return userRepository.findAll();
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
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.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
|
||||
@@ -12,15 +16,32 @@ 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,
|
||||
public UserDto registerUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.registerUser(name, password);
|
||||
}
|
||||
|
||||
@RequestMapping("/login")
|
||||
public UserDTO login(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.login(name, password);
|
||||
@RequestMapping("/loginUser")
|
||||
public UserDto loginUser(@RequestParam(value = "name") String name,
|
||||
@RequestParam(value = "password") String password) {
|
||||
return userService.loginUser(name, password);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}
|
||||
@@ -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,15 +21,15 @@ 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) {
|
||||
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());
|
||||
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 login(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,6 +47,22 @@ 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
|
||||
*/
|
||||
public void addVeganMeal(Long id, String name) {
|
||||
User user = userRepository.findByName(name);
|
||||
int count = user.getVeganMeal();
|
||||
count++;
|
||||
user.setVeganMeal(count);
|
||||
userRepository.save(user);
|
||||
logger.info("Added vegan meal to user(id=" + user.getId()
|
||||
+ ", name=" + user.getName() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
6
src/Server/src/main/resources/application.properties
Normal file
6
src/Server/src/main/resources/application.properties
Normal file
@@ -0,0 +1,6 @@
|
||||
spring.datasource.url=jdbc:h2:file:~/spring-boot-h2.db;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.h2.console.enabled=true
|
||||
@@ -1,16 +1,12 @@
|
||||
import greenify.server.Application;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootConfiguration
|
||||
public class ApplicationTest {
|
||||
@Test
|
||||
public void applicationContextLoaded() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void applicationContextTest() {
|
||||
Application.main(new String[] {});
|
||||
}
|
||||
}
|
||||
public void contextLoads() throws Exception{ }
|
||||
}
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
import greenify.common.UserDTO;
|
||||
import greenify.server.Application;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.*;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.web.client.HttpStatusCodeException;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
public class UserControllerTest {
|
||||
private static Logger logger = LoggerFactory.getLogger(UserControllerTest.class);
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
private RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
@Test
|
||||
public void registerUserTest() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + port + "/registerUser")
|
||||
.queryParam("name", "ceren")
|
||||
.queryParam("password", "password");
|
||||
HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||
UserDTO user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
|
||||
Assert.assertNotNull(user);
|
||||
Assert.assertEquals(user.getId().longValue(), 1L);
|
||||
try {
|
||||
user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
|
||||
Assert.fail("Error should be reported");
|
||||
} catch (HttpStatusCodeException exception) {
|
||||
int statusCode = exception.getStatusCode().value();
|
||||
Assert.assertEquals(statusCode, 400);
|
||||
Assert.assertTrue(exception.getResponseBodyAsString().contains("User already exists"));
|
||||
}
|
||||
}
|
||||
}
|
||||
//import greenify.common.UserDTO;
|
||||
//import greenify.server.Application;
|
||||
//import org.junit.Assert;
|
||||
//import org.junit.Test;
|
||||
//import org.junit.runner.RunWith;
|
||||
//import org.slf4j.Logger;
|
||||
//import org.slf4j.LoggerFactory;
|
||||
//import org.springframework.boot.web.server.LocalServerPort;
|
||||
//import org.springframework.boot.test.context.SpringBootTest;
|
||||
//import org.springframework.http.*;
|
||||
//import org.springframework.test.context.junit4.SpringRunner;
|
||||
//import org.springframework.web.client.HttpStatusCodeException;
|
||||
//import org.springframework.web.client.RestTemplate;
|
||||
//import org.springframework.web.util.UriComponentsBuilder;
|
||||
//
|
||||
//@RunWith(SpringRunner.class)
|
||||
//@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
//public class UserControllerTest {
|
||||
// private static Logger logger = LoggerFactory.getLogger(UserControllerTest.class);
|
||||
//
|
||||
// @LocalServerPort
|
||||
// private int port;
|
||||
//
|
||||
// private RestTemplate restTemplate = new RestTemplate();
|
||||
//
|
||||
// @Test
|
||||
// public void registerUserTest() {
|
||||
// HttpHeaders headers = new HttpHeaders();
|
||||
// headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
||||
// UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:" + port + "/registerUser")
|
||||
// .queryParam("name", "ceren")
|
||||
// .queryParam("password", "password");
|
||||
// HttpEntity<?> entity = new HttpEntity<>(headers);
|
||||
// UserDTO user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
|
||||
// Assert.assertNotNull(user);
|
||||
// Assert.assertEquals(user.getId().longValue(), 1L);
|
||||
// try {
|
||||
// user = this.restTemplate.getForObject(builder.build().encode().toUri(), UserDTO.class);
|
||||
// Assert.fail("Error should be reported");
|
||||
// } catch (HttpStatusCodeException exception) {
|
||||
// int statusCode = exception.getStatusCode().value();
|
||||
// Assert.assertEquals(statusCode, 400);
|
||||
// Assert.assertTrue(exception.getResponseBodyAsString().contains("User already exists"));
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -55,3 +55,4 @@ public class UserTest {
|
||||
assertTrue(first.hashCode() == second.hashCode());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
package greenify.server.data.model;
|
||||
|
||||
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 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");
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,63 @@
|
||||
//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())));
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,70 @@
|
||||
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.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;
|
||||
|
||||
@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;
|
||||
|
||||
/**
|
||||
* setUp method for test.
|
||||
*/
|
||||
@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);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user