apparently the changes that were supposed to be made weren't committed in my previous commit, so here is the right one
REMOVE:: removed the .gradle and build folders from the Client, Server and Common folders, also moved the contents that was in the src folders of these folders, to the folders themselves and deleted the src folders EDIT:: edited the build.gradle file, build should now work combined the build.gradle files in the Client, Server and Common folders into the build.gradle file in the root
This commit is contained in:
13
src/Server/main/java/gogreen/server/Application.java
Normal file
13
src/Server/main/java/gogreen/server/Application.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package gogreen.server;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
22
src/Server/main/java/gogreen/server/data/model/User.java
Normal file
22
src/Server/main/java/gogreen/server/data/model/User.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package gogreen.server.data.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
Long id;
|
||||
String name;
|
||||
String password;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package gogreen.server.data.repository;
|
||||
|
||||
import gogreen.server.data.model.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserJpaRepository extends gogreen.server.data.repository.UserRepository, JpaRepository<User,Long> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package gogreen.server.data.repository;
|
||||
|
||||
import gogreen.server.data.model.User;
|
||||
|
||||
public interface UserRepository {
|
||||
User findByName(String name);
|
||||
<T extends User> T save(T user);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package gogreen.server.rest;
|
||||
|
||||
import gogreen.common.ApplicationException;
|
||||
import gogreen.common.ErrorResponse;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class RestExceptionHandler {
|
||||
@ExceptionHandler(ApplicationException.class)
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public ErrorResponse applicationException (ApplicationException ex) {
|
||||
return new ErrorResponse(ex.getMessage());
|
||||
}
|
||||
}
|
||||
26
src/Server/main/java/gogreen/server/rest/UserController.java
Normal file
26
src/Server/main/java/gogreen/server/rest/UserController.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package gogreen.server.rest;
|
||||
|
||||
import gogreen.common.UserDTO;
|
||||
import gogreen.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;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@RequestMapping("/registerUser")
|
||||
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);
|
||||
}
|
||||
}
|
||||
40
src/Server/main/java/gogreen/server/service/UserService.java
Normal file
40
src/Server/main/java/gogreen/server/service/UserService.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package gogreen.server.service;
|
||||
|
||||
import gogreen.common.ApplicationException;
|
||||
import gogreen.common.UserDTO;
|
||||
import gogreen.server.data.model.User;
|
||||
import gogreen.server.data.repository.UserRepository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class UserService {
|
||||
Logger logger = LoggerFactory.getLogger(UserService.class);
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
public UserDTO registerUser(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
if (user != null) {
|
||||
throw new ApplicationException("User already exists");
|
||||
} else {
|
||||
user = userRepository.save(new User(null, name, password));
|
||||
}
|
||||
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
|
||||
return new UserDTO(user.getId(), user.getName());
|
||||
}
|
||||
|
||||
public UserDTO login(String name, String password) {
|
||||
User user = userRepository.findByName(name);
|
||||
if (user == null) {
|
||||
throw new ApplicationException("User does not exist");
|
||||
} else {
|
||||
if (!user.getPassword().equals(password)) {
|
||||
throw new ApplicationException("Wrong password");
|
||||
}
|
||||
}
|
||||
return new UserDTO(user.getId(), user.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package gogreen.server.rest;
|
||||
|
||||
import gogreen.common.UserDTO;
|
||||
import gogreen.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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user