EDIT:: Reimported project
reimported project to fix the errors in client application FIX:: fixed the "location is not set" error in client application when loading fxml file reimporting the project made it work, as it now has a submodule folder structure EDIT:: edited settings.gradle file to include the modules in the new structure EDIT: edited client build.gradle and server build.gradle files to pick the right common folder (changed complie(:Common) to compile(:src:Common))
This commit is contained in:
73
src/Server/build.gradle
Normal file
73
src/Server/build.gradle
Normal file
@@ -0,0 +1,73 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
dependencies {
|
||||
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'eclipse'
|
||||
apply plugin: 'idea'
|
||||
apply plugin: 'org.springframework.boot'
|
||||
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'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
sourceCompatibility = 1.8
|
||||
targetCompatibility = 1.8
|
||||
|
||||
dependencies {
|
||||
compile("org.springframework.boot:spring-boot-starter-web")
|
||||
testCompile('org.springframework.boot:spring-boot-starter-test')
|
||||
compile project(':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")
|
||||
testCompile(
|
||||
'junit:junit:4.12',
|
||||
'org.junit.jupiter:junit-jupiter-api:5.4.0'
|
||||
)
|
||||
testRuntime(
|
||||
'org.junit.jupiter:junit-jupiter-engine:5.4.0',
|
||||
'org.junit.vintage:junit-vintage-engine:5.4.0'
|
||||
)
|
||||
}
|
||||
|
||||
jacoco {
|
||||
toolVersion = "0.8.2"
|
||||
reportsDir = file("$buildDir/customJacocoReportDir")
|
||||
}
|
||||
|
||||
jacocoTestReport {
|
||||
reports {
|
||||
xml.enabled false
|
||||
csv.enabled false
|
||||
html.destination file("${buildDir}/jacocoHtml")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
13
src/Server/src/main/java/gogreen/server/Application.java
Normal file
13
src/Server/src/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);
|
||||
}
|
||||
}
|
||||
|
||||
40
src/Server/src/main/java/gogreen/server/data/model/User.java
Normal file
40
src/Server/src/main/java/gogreen/server/data/model/User.java
Normal file
@@ -0,0 +1,40 @@
|
||||
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;
|
||||
|
||||
public User(Long id, String name, String password) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return 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 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());
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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