FIX:: fixed Client application class

The class was not seeing some imports from the same package, and some getters and setters for User.java were not written, so I wrote those and fixed the imports

FIX:: fixed packages

Everything now isn't in several subpackages in the Client, Common and Server package anymore, but straight in their respectable packages. Also fixed the package names

BUG FIX: fixed Client Application class not being able to see GetBean method

Don't really know how it got fixed, but resolving the import issues also fixed this bug.

EDIT::changed main class in build.gradle file to Client.Application

---------------
I now only get a bug where the fxmlloader can't see the fxml files, and thus I can't start the application. I get the error message "location not set" although I used fxmlloader.setLocation.
This commit is contained in:
Sem van der Hoeven
2019-03-07 20:46:51 +01:00
parent aa79365784
commit cab6d9aeeb
22 changed files with 67 additions and 49 deletions

View File

@@ -1,13 +0,0 @@
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);
}
}

View File

@@ -1,22 +0,0 @@
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;
}

View File

@@ -1,8 +0,0 @@
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> {
}

View File

@@ -1,8 +0,0 @@
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);
}

View File

@@ -1,17 +0,0 @@
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());
}
}

View File

@@ -1,26 +0,0 @@
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);
}
}

View File

@@ -1,40 +0,0 @@
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());
}
}

View File

@@ -1,48 +0,0 @@
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"));
}
}
}