Google App Engine User Registration System - java

I am creating a user registration system in google app engine, and so far I could develop the registration side of the app. But I can't log in and create a HttpSession.
Here what I did, I created login.jsp and two text boxes to input email and password.
And then I created Bean class and DAO class as following.
package com.myfirstguide.beans;
import javax.servlet.http.HttpSession;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
public class UserDAO {
public static UserBean login(UserBean bean){
String email = bean.getEmail();
String password = bean.getPassword();
try{
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Query q = new Query("Account");
q.addFilter("email", Query.FilterOperator.EQUAL, email);
q.addFilter("password", Query.FilterOperator.EQUAL, password);
PreparedQuery pq = datastore.prepare(q);
Entity result = pq.asSingleEntity();
if(result != null){
bean.setValid(true);
}
//return bean;
}catch(Exception e){
}
return bean;
}
}
Here is the Bean class.
package com.myfirstguide.beans;
public class UserBean {
private String username;
private String password;
private String firstName;
private String lastName;
private String email;
public boolean valid;
public String getFirstName() {
return firstName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public void setEmail(String semail){
email = semail;
}
public String getEmail(){
return email;
}
public String getLastName() {
return lastName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
public String getPassword() {
return password;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getUsername() {
return username;
}
public void setUserName(String newUsername) {
username = newUsername;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean newValid) {
valid = newValid;
}
}
And here is the Servlet.
package com.myfirstguide.users;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.myfirstguide.beans.UserBean;
import com.myfirstguide.beans.UserDAO;
public class SignIn extends HttpServlet{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String email = req.getParameter("email");
String password = req.getParameter("password");
try{
UserBean ubean = new UserBean();
ubean.setEmail(email);
ubean.setPassword(password);
ubean = UserDAO.login(ubean);
if(ubean.isValid()){
HttpSession session = req.getSession(true);
session.setAttribute("currentSessionUser",ubean);
resp.sendRedirect("index.jsp?session=" + ubean.getFirstName()); //logged-in page
}
}catch(Throwable e){
System.out.println(e);
}
}
}
And I don't know JPA. And if there is better way to do this, please tell me.
Thank you!

Have you enabled sessions?
https://developers.google.com/appengine/docs/java/config/appconfig#Enabling_Sessions

Related

User Update Test in the Junit Doesn't Work

I'm trying make a test to Update User method that first get a User by id and after update the user , work it, but when I try to make a test in the Junit show me that couldn't find the user by Id. Can anyone help me, please?
UserClass
package br.com.projectsmanagement.entities;
import java.util.Date;
import java.util.Objects;
import jakarta.persistence.*;
#Entity
#Table(name = "tb_users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
private String password;
#Temporal(TemporalType.TIMESTAMP)
private Date dataCadastro;
public User() {
}
public User(String name, String email, String password, Date dataCadastro) {
this.name = name;
this.email = email;
this.password = password;
this.dataCadastro = dataCadastro;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
#Override
public int hashCode() {
return Objects.hash(id);
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
return Objects.equals(id, other.id);
}
}
UserServiceImpl
#Override
public User updateUser(Long id, User user) {
try {
User userUpdated = userRepository.findById(id).get();
userUpdated.setName(user.getName());
userUpdated.setPassword(user.getPassword());
return userRepository.saveAndFlush(userUpdated);
} catch (NoSuchElementException e) {
throw new InvalidIdException("Usuário não encontrado!");
}
}
And finally my UserServiceImplTest
package br.com.projectsmanagement.services.impl;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.context.SpringBootTest;
import br.com.projectsmanagement.entities.User;
import br.com.projectsmanagement.exception.EmailExistException;
import br.com.projectsmanagement.repositories.UserRepository;
#SpringBootTest
class UserServiceImplTest {
#InjectMocks
private UserServiceImpl userServiceImpl;
#Mock
private UserRepository userRepository;
private User user;
private Optional<User> optionalUser;
#BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
startUser();
}
#Test
void whenFindAllThenReturnAnListofUsers() {
when(userRepository.findAll()).thenReturn(List.of(user));
List<User> response = userServiceImpl.listUsers();
assertNotNull(response);
assertEquals(1, response.size());
assertEquals(User.class, response.get(0).getClass());
}
#Test
void whenFindByIdThenReturnAnUserInstance() {
when(userRepository.findById(anyLong())).thenReturn(optionalUser);
Optional<User> response = userServiceImpl.getUserById(1L);
assertThat(response).isPresent();
assertEquals(optionalUser.getClass(), response.getClass());
assertEquals(optionalUser.get().getName(), "Valdir");
}
#Test
void whenRegisterThenReturnSuccess() {
when(userRepository.saveAndFlush(any())).thenReturn(user);
User response = userServiceImpl.registerUser(user);
assertNotNull(response);
assertEquals(User.class, response.getClass());
assertEquals(user.getId(), response.getId());
}
#Test
void whenRegisterThenReturnAnEmailExistException() {
when(userRepository.findByEmail(anyString())).thenReturn(user);
try {
userServiceImpl.registerUser(user);
} catch (ClassCastException e) {
assertEquals(EmailExistException.class, e.getClass());
assertEquals("Esse e-mail já está cadastrado!", e.getMessage());
}
User response = userServiceImpl.registerUser(user);
assertNotNull(response);
assertEquals(User.class, response.getClass());
assertEquals(user.getId(), response.getId());
}
#Test
void whenUpdateThenReturnSuccess() {
when(userRepository.saveAndFlush(any())).thenReturn(user);
User response = userServiceImpl.updateUser(1L, user);
assertNotNull(response);
assertEquals(User.class, response.getClass());
}
#Test
void testDeleteUser() {
fail("Not yet implemented");
}
private void startUser() {
user = new User("Valdir", "valdir#gmail.com", "123456", new Date());
optionalUser = Optional.of(new User("Valdir", "valdir#gmail.com", "123456", new Date()));
}
}
I modified my class many times, but even doesn't work it
I think in your test case whenUpdateThenReturnSuccess(), you forgot to mock the userRepository.findById(id) that you call when updating your user.
If not mocked, it returns null.
#Test
void whenUpdateThenReturnSuccess() {
when(userRepository.findById(anyLong())).thenReturn(optionalUser);
when(userRepository.saveAndFlush(any(User.class))).thenReturn(user);
User response = userServiceImpl.updateUser(1L, user);
assertNotNull(response);
assertEquals(User.class, response.getClass());
}

Hibernate does not insert the information into the database

I'm trying to sort of login with JPA and hibernate. The truth is that I'm new to working with hibernate and I don't know what I'm doing wrong.
I have two entity classes (User and Credential), and I have already mapped them in the persistence.xml file
<!-- Define Persistence Unit -->
<persistence-unit name="HibernateSchedule" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>ues.edu.sv.entity.User</class>
<class>ues.edu.sv.entity.Credential</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/diary?useSSL=false&useTimezone=true&serverTimezon=UTC&allowPublicKeyRetrieval=true"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="localhost"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
These are the entity classes:
package ues.edu.sv.entity;
import java.io.Serializable;
import javax.persistence.*;
#Entity
#NamedQueries({
#NamedQuery(name="User.listAllUsers", query="SELECT u FROM User u")
})
#Table(name = "\"user\"")
public class User implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id_user")
private int idUser;
private String name;
#Column(name = "lastname")
private String lastName;
private String email;
public User(){}
public User(int idUser){
this.idUser = idUser;
}
public User(String name, String lastName, String email){
this.name = name;
this.lastName = lastName;
this.email = email;
}
public User(int idUser, String name, String lastName, String email){
this.idUser = idUser;
this.name = name;
this.lastName = lastName;
this.email = email;
}
public int getIdUser() {
return idUser;
}
public void setIdUser(int idUser) {
this.idUser = idUser;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public String toString() {
return "User{" + "idUser=" + idUser + ", name=" + name + ", lastName=" + lastName + ", email=" + email + '}';
}
}
This is the Credential class
package ues.edu.sv.entity;
import java.io.Serializable;
import javax.persistence.*;
#Entity
#NamedQueries({
#NamedQuery(name = "Credential.getAllCredentials", query = "SELECT c FROM Credential
c")
})
#Table(name = "credential")
public class Credential implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name = "id_credential")
private int idCredential;
#Column(name = "username")
private String userName;
private String password;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "id_user", referencedColumnName = "id_user")
private User user;
public Credential(){}
public Credential(int idCredential){
this.idCredential = idCredential;
}
public Credential(String userName, String password){
this.userName = userName;
this.password = password;
}
public Credential(String userName, String password, User user){
this.userName = userName;
this.password = password;
this.user = user;
}
public Credential(int idCredential,String userName, String password){
this.idCredential = idCredential;
this.userName = userName;
this.password = password;
}
public int getIdCredential() {
return idCredential;
}
public void setIdCredential(int idCredential) {
this.idCredential = idCredential;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
#Override
public String toString() {
return "Credential{" + "idCredential=" + idCredential + ", userName=" + userName + ", password=" + password + ", user=" + user + '}';
}
}
I am using a three layer pattern to retrieve the information, so I have a package "ues.edu.sv.dao" where I have two interfaces and two classes that implement them and are defined as ejb
package ues.edu.sv.dao;
import java.util.List;
import ues.edu.sv.entity.User;
public interface UserDao {
public List<User> listAllUsers();
public User getUserById(User user);
public void createNewUser(User user);
public void updateUser(User user);
public void deleteUser(User user);
}
package ues.edu.sv.dao;
import java.util.List;
import ues.edu.sv.entity.Credential;
public interface CredentialDao {
public List<Credential> getAllCredentials();
public Credential getCredentialById(Credential credential);
public Credential getCredentialByUserName(Credential credential);
public void createNewCredential(Credential credential);
public void updateCredential(Credential credential);
public void deleteCredential(Credential credential);
}
And here are the classes that implement their corresponding interface
package ues.edu.sv.dao;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.*;
import ues.edu.sv.entity.User;
#Stateless
public class UserDaoImpl implements UserDao {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("HibernateSchedule");
EntityManager em = emf.createEntityManager();
#Override
public List<User> listAllUsers() {
return em.createNamedQuery("User.listAllUsers").getResultList();
}
#Override
public User getUserById(User user) {
return em.find(User.class, user.getIdUser());
}
#Override
public void createNewUser(User user) {
em.persist(user);
}
#Override
public void updateUser(User user) {
em.merge(user);
}
#Override
public void deleteUser(User user) {
em.remove(em.merge(user));
}
}
package ues.edu.sv.dao;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import ues.edu.sv.entity.Credential;
#Stateless
public class CredentialDaoImpl implements CredentialDao {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("HibernateSchedule");
EntityManager em = emf.createEntityManager();
#Override
public List<Credential> getAllCredentials() {
return em.createNamedQuery("Credential.getAllCredentials").getResultList();
}
#Override
public Credential getCredentialById(Credential credential) {
return em.find(Credential.class, credential.getIdCredential());
}
#Override
public Credential getCredentialByUserName(Credential credential) {
return em.find(Credential.class, credential.getUserName());
}
#Override
public void createNewCredential(Credential credential) {
em.persist(credential);
}
#Override
public void updateCredential(Credential credential) {
em.merge(credential);
}
#Override
public void deleteCredential(Credential credential) {
em.remove(em.merge(credential));
}
}
My understanding is that since I am working in a java enterprise context, there is no need to open a transaction when persisting an object in the database. I am using glassfish by the way.
now to complete the three-layer pattern, I have another package called "ues.edu.sv.service" that injects the above interfaces via CDI
package ues.edu.sv.service;
import java.util.List;
import javax.ejb.Local;
import ues.edu.sv.entity.Credential;
#Local
public interface CredentialService {
public List<Credential> getAllCredentials();
public Credential getCredentialById(Credential credential);
public Credential getCredentialByUserName(Credential credential);
public void createNewCredential(Credential credential);
public void updateCredential(Credential credential);
public void deleteCredential(Credential credential);
}
package ues.edu.sv.service;
import java.util.List;
import javax.ejb.Local;
import ues.edu.sv.entity.User;
#Local
public interface UserService {
public List<User> listAllUsers();
public User getUserById(User user);
public void createNewUser(User user);
public void updateUser(User user);
public void deleteUser(User user);
}
and implementation classes
package ues.edu.sv.service;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.inject.Inject;
import ues.edu.sv.dao.CredentialDao;
import ues.edu.sv.entity.Credential;
#Stateless
public class CredentialServiceImpl implements CredentialService{
#Inject
CredentialDao credentialDao;
#Resource
SessionContext context;
#Override
public List<Credential> getAllCredentials() {
return credentialDao.getAllCredentials();
}
#Override
public Credential getCredentialById(Credential credential) {
return credentialDao.getCredentialById(credential);
}
#Override
public Credential getCredentialByUserName(Credential credential) {
return credentialDao.getCredentialByUserName(credential);
}
#Override
public void createNewCredential(Credential credential) {
credentialDao.createNewCredential(credential);
}
#Override
public void updateCredential(Credential credential) {
try{
credentialDao.updateCredential(credential);
}catch(Exception ex){
ex.printStackTrace(System.out);
context.setRollbackOnly();
}
}
#Override
public void deleteCredential(Credential credential) {
credentialDao.deleteCredential(credential);
}
}
package ues.edu.sv.service;
import java.util.List;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.inject.Inject;
import ues.edu.sv.dao.UserDao;
import ues.edu.sv.entity.User;
#Stateless
public class UserServiceImpl implements UserService{
#Inject
UserDao userDao;
#Resource
SessionContext context;
#Override
public List<User> listAllUsers() {
return userDao.listAllUsers();
}
#Override
public User getUserById(User user) {
return userDao.getUserById(user);
}
#Override
public void createNewUser(User user) {
userDao.createNewUser(user);
}
#Override
public void updateUser(User user) {
try{
userDao.updateUser(user);
}catch(Exception ex){
ex.printStackTrace(System.out);
context.setRollbackOnly();
}
}
#Override
public void deleteUser(User user) {
userDao.deleteUser(user);
}
}
And finally I have an ejb that connects to the jsf page called "Register.xhtml" and is responsible for retrieving the information that the user enters in the text fields to finally persist the information in the database, I have programmed the relationships so cascading user object can be persisted
package ues.edu.sv.beans;
import java.io.IOException;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import ues.edu.sv.crypto.Cypher;
import ues.edu.sv.entity.Credential;
import ues.edu.sv.entity.User;
import ues.edu.sv.service.CredentialService;
import ues.edu.sv.service.UserService;
#Named
#RequestScoped
public class RegisterBean {
#Inject
CredentialService credentialService;
#Inject
UserService userService;
private String name;
private String lastName;
private String email;
private String userName;
private String password;
public RegisterBean() {
}
public void toRegister() {
User user = new User(name, lastName, email);
String encriptedPassword = Cypher.encryptPassword(password);
Credential credential = new Credential(userName, encriptedPassword, user);
credentialService.createNewCredential(credential);
try {
FacesContext.getCurrentInstance().getExternalContext().redirect("Login.xhtml");
} catch (IOException ex) {
ex.printStackTrace(System.out);
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
And apparently everything is fine, the console does not show me any error, but it does nothing in the database, it does not insert the content into the database :(
This is what appears in the console when everything has been executed...
]|#]
HHH000204: Processing PersistenceUnitInfo [name: HibernateSchedule]|#]
HHH000412: Hibernate Core {6.0.0.Alpha4}|#]
HCANN000001: Hibernate Commons Annotations {5.1.0.Final}|#]
HHH10001002: Using Hibernate built-in connection pool (not for production use!)|#]
HHH10001005: using driver [com.mysql.cj.jdbc.Driver] at URL
[jdbc:mysql://localhost:3306/diary?
useSSL=false&useTimezone=true&serverTimezon=UTC&allowPublicKeyRetrieval=true]|#]
HHH10001001: Connection properties: {user=root, password=****}|#]
HHH10001003: Autocommit mode: false|#]
HHH000115: Hibernate connection pool size: 20 (min=1)|#]
HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect|#]
HHH10005002: No explicit CDI BeanManager reference was passed to Hibernate, but CDI
is available on the Hibernate ClassLoader.|#]
HHH000490: Using JtaPlatform implementation:
[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]|#]
That is all that appears. Someone who can help me please

Gradle test with Quarkus Framework

The following code is give: User/UserResources/UserService
I wrote: Schritt3.java
I have to write a test, but I am completely lost. I googled so much and did not come closer to a Solution.
The first try get´s this error: RESTEASY003320: Failed processing arguments of public java.util.List de.hse.swa.jaxquarkus.step3.UserResource.addUser(java.lang.String,java.lang.String,java.lang.String,java.lang.Boolean)
The second one thise on: Expected status code <200> but was <400>.
As you can see I have tried different encoding types and ways to parse data. But googeling the error messages did not help me at all. I don´t know if I am just on the wrong path or if there is something else wrong.
So the question is: How to correctly pass the data.
User.java
import javax.ws.rs.FormParam;
public class User {
private Long id;
#FormParam("username")
private String username;
#FormParam("password")
private String password;
#FormParam("firstName")
private String fullName;
#FormParam("isAdmin")
private boolean isAdmin = false;
public User() {
}
public User(String username, String password, String fullName, boolean isAdmin) {
this.username = username;
this.password = password;
this.fullName = fullName;
this.isAdmin = isAdmin;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
// #JsonIgnore()
public String getPassword() {
return password;
}
// #JsonProperty()
public void setPassword(String password) {
this.password = password;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean admin) {
isAdmin = admin;
}
}
UserResource.java
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.annotations.Form;
import io.vertx.core.http.HttpServerRequest;
#RequestScoped
#Path("/step3/users")
public class UserResource {
#Inject
UserService service;
#Context
HttpServerRequest request;
#GET
#Produces("application/json")
public List<User> greeting() {
return service.getUsers();
}
#PUT
#Produces("application/json")
#Consumes("application/json")
public List<User> addUser(
#FormParam("username") String username,
#FormParam("password") String password,
#FormParam("fullName") String fullName,
#FormParam("isAdmin") Boolean isAdmin) {
User user = new User(username, password, fullName, isAdmin);
return service.addUser(user);
}
#POST
#Consumes("application/json")
#Produces("application/json")
public List<User> updateUser(#Form User form) {
User user = new User(form.getUsername(), form.getPassword(),
form.getFullName(), form.isAdmin());
return service.addUser(user);
}
}
User Service.java
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
#ApplicationScoped
public class UserService {
public static List<User> users = new ArrayList<User>();
public static Long id = 1L;
public List<User> getUsers() {
return users;
}
public List<User> addUser(User user) {
user.setId(id++);
users.add(user);
return users;
}
public List<User> updateUser(User user) {
for (int index = 0; index < users.size(); ++index) {
if (users.get(index).getId().equals(user.getId())) {
users.set(index, user);
break;
}
}
return users;
}
public List<User> removeUser(User user) {
for (int index = 0; index < users.size(); ++index) {
if (users.get(index).getId().equals(user.getId())) {
users.remove(index);
break;
}
}
return users;
}
}
Schritt3.java
package de.hse.swa.jaxquarkus;
import de.hse.swa.jaxquarkus.step3.User;
import static org.hamcrest.CoreMatchers.containsString;
import javax.ws.rs.core.MediaType;
import io.quarkus.test.junit.QuarkusTest;
//import io.restassured.RestAssured;
import io.restassured.response.Response;
//import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import static io.restassured.RestAssured.given;
#QuarkusTest
public class Schritt3 {
String username = "test";
String password = "123";
String fullName = "testing";
Boolean isAdmin = false;
private static String requestBody = "{\n" +
" \"username\": \"123\",\n" +
" \"password\": \"456\",\n" +
" \"fullName\": \"789\",\n" +
" \"isAdmin\": \"true\" \n}";
#Test
public void postRequest()
{
/* First Try
Response response = given()
.header("Content-type", "application/json")
.contentType(ContentType.JSON)
//.body(requestBody)
.body(username + password + fullName + isAdmin)
.when()
.put("/step3/users")
.then()
.extract().response();
System.out.println("Respone from Step3 is:");
System.out.println(response.asString());
// Assertions.assertEquals("1234", response.jsonPath().getString("username"));
*/
/* Second Try
*/
RestAssured.baseURI = "http://localhost:8080";
given().urlEncodingEnabled(true)
.param("username", "user#site.com")
.param("password", "Pas54321")
.param("fullName", "MAtze")
.param("isAdmin", "true")
//.header("Accept", ContentType.JSON.getAcceptHeader())
// .header("Content-type", "application/json")
.contentType("application/json")
.put("/step3/users")
.then().statusCode(200);
}
}
Long story short:
The Code I was given works not as intended. To solve the Problem I had to change the Consume to:
#Consumes("application/x-www-form-urlencoded")
Then I could simply send the Request via formParam:
Response response =
given()
.formParam("username", "username")
.formParam("password", "password")
.formParam("fullName", "fullName")
.formParam("isAdmin", true)
.when()
.put("/step3/users")
.then()
.extract().response();
The Test can then be done via Assertions.
Assertions.assertEquals("username", response.jsonPath().getString("username"));
The only problem remaining is, that the returned Json Object starts and ends with []. So if I am checking for the username I have to add the brackets for the test so succeed.

Data not sent to MySQL database while using Spring

I am new to Spring Framework and java. I was creating Registration Module for my Web App. I created DAO, DTO, Dispatcher Servlet, controller and all. When I submit the form it redirects me to where I want to go but no data are added to the Database..
My userDTO.java
package com.lagan.dto;
public class userDto{
public String userId;
public String firstName;
public String lastName;
public String birthDate;
public int gender;
public String email;
public String password;
public String getUserId() {
return userId;
}
public String getUserId(String userId) {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getBirthDate() {
return birthDate;
}
public void setBirthDate(String birthDate) {
this.birthDate = birthDate;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password){
this.password = password;
}
}
userDao.java
package com.lagan.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.lagan.dbconnector.dbconnect;
import com.lagan.dto.userDto;
public class userDao {
public void create (userDto user) {
dbconnect db=new dbconnect();
Connection con=null;
try{
con=db.getConnections();
String sql="insert into tbluser values(?,?,?,?,?,?,?)";
PreparedStatement pre=con.prepareStatement(sql);
pre.setString(1,user.getUserId());
pre.setString(2,user.getFirstName());
pre.setString(3,user.getLastName());
pre.setString(4,user.getBirthDate());
pre.setInt(5,user.getGender());
pre.setString(6,user.getEmail());
pre.setString(7,user.getPassword());
pre.executeUpdate();
pre.close();
}catch (Exception e) {
e.printStackTrace();
}finally {
db.closeConnection(con);
}
}
}
userController.java
package com.lagan.controller;
import java.util.List;
import com.lagan.dao.userDao;
import com.lagan.dto.userDto;
public class userController {
public void create(userDto userDto){
userDao dao=new userDao();
dao.create(userDto);
}
}
Verification.jsp
<%# page import="com.lagan.controller.*,com.lagan.dto.*"%>
<%
String userId="aqwery";
String firstName=request.getParameter("firstName");
String lastName=request.getParameter("lastName");
String birthDate=request.getParameter("birthDate");
//int gender=request.getParameter("gender");
String email=request.getParameter("email");
String password=request.getParameter("password");
userDto dto=new userDto();
dto.getUserId(userId);
dto.setFirstName(firstName);
dto.setLastName(lastName);
dto.setBirthDate(birthDate);
//dto.setGender(gender);
dto.setEmail(email);
dto.setPassword(password);
userController uC=new userController();
uC.create(dto);
response.sendRedirect("home.html");
%>
dbconnect.java
package com.lagan.dbconnector;
import java.sql.*;
//import java.sql.Connection;
//import java.sql.DriverManager;
//import java.sql.SQLException;
public class dbconnect {
public dbconnect()
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnections() {
Connection conn=null;
try{
conn=DriverManager.getConnection("jdbc:mysql://localhost:80/project_lagan","root","");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void closeConnection(Connection con){
if(null!=con) {
try{
con.close();
}catch(SQLException e){}
}
}
}
It redirects me to home.html but the datas are not added to the database. I don't know what i am doing wrong. Please Help.
Try changing the port number of connection string to either of these:
`jdbc:mysql://localhost:3306/project_lagan`
or
`jdbc:mysql://localhost/project_lagan`
I will suggest you connecting like that
String url = "jdbc:mysql://localhost:3306/project_lagan";
String user = "root";
String password = "";
Connection conn = null;
try(conn = DriverManager.getConnection(url, user, password);) {
// processing here
} catch(SQLException e) {
System.out.println(e.getMessage());
}
You can check this tutorial also.

Spring MVC application

Hi all I´m learning how to use Spring, I don´t have experience in MVC.
So I´m making a website who makes registrations, de-registrations and changes in the data of a mysql database.
The login and inserts to DB are ready but I can't make the delete registered user part.
My model:
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.NotEmpty;
public class StudentDelete {
#NotEmpty
#Size(min=4, max=20)
private String userName;
#NotEmpty
#Size(min=4, max=8)
private String password;
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
My Controller:
#Controller
#SessionAttributes("student")
public class StudentController {
#Autowired
private StudentService studentService;
#RequestMapping(value="/delete", method=RequestMethod.GET)
public String delete(Model model) {
Student studentDelete = new Student();
model.addAttribute("studentDelete", studentDelete);
return "delete";
}
blablabla
#RequestMapping(value="/delete", method=RequestMethod.POST)
public String delete(#Valid #ModelAttribute("studentDelete") StudentDelete studentDelete, BindingResult result) {
if (result.hasErrors()) {
return "delete";
} else {
boolean found = studentService.findByLogin(studentDelete.getUserName(), studentDelete.getPassword());
if (found) {
studentService.deleteByLogin(studentDelete.getUserName(), studentDelete.getPassword());
return "successD";
} else {
return "failureD";
}
}
}
My Service and the implementation:
package com.github.elizabetht.service;
import com.github.elizabetht.model.Student;
public interface StudentService {
Student save(Student student);
boolean findByLogin(String userName, String password);
boolean findByUserName(String userName);
boolean deleteByLogin(String userName, String password);
}
Implementation:
public boolean deleteByLogin(String userName, String password) {
StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);
if(stud != null) {
return true;
}
return false;
}
And finally The StudentDeleteRepository:
package com.github.elizabetht.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.github.elizabetht.model.Student;
import com.github.elizabetht.model.StudentDelete;
#Repository("studentDeleteRepository")
public interface StudentDeleteRepository extends JpaRepository<StudentDelete, Long> {
#Query("delete s from Student s where s.userName = :userName and s.password = :password")
StudentDelete deleteByLogin(#Param("userName") String userName, #Param("password") String password);
}
StudentRepository.java
package com.github.elizabetht.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.github.elizabetht.model.Student;
#Repository("studentRepository")
public interface StudentRepository extends JpaRepository<Student, Long> {
#Query("select s from Student s where s.userName = :userName")
Student findByUserName(#Param("userName") String userName);
}
In my delete.jsp all starts with this:
<form:form id="myForm" method="post"
class="bs-example form-horizontal" commandName="studentDelete">
I'm getting this error:
java.lang.NullPointerException
com.github.elizabetht.service.StudentServiceImpl.deleteByLogin(StudentServiceImpl.java:47)
Which is this part:
StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);
Why it doesnt happen with the save method?
Any help is appreciated.
Thanks!
Your issue is that parameter you are passing below:
StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);
username or password are empty.
Hence the null pointer exception.
Change your implementation to following formate:
public boolean deleteByLogin(String userName, String password) {
if(userName == null || password == null)
return false; // it failed essentially
StudentDelete stud = studentDeleteRepository.deleteByLogin(userName, password);
if(stud != null) {
return true;
}
return false;
}
I think the error you specified in the comments:
Not a managed type: class com.github.elizabetht.model.StudentDelete
is because you are missing the JPA #Entity annotation on your model class:
import javax.validation.constraints.Size;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.validator.constraints.NotEmpty;
#Entity
#Table("student")
public class StudentDelete {
#NotEmpty
#Size(min=4, max=20)
private String userName;
#NotEmpty
#Size(min=4, max=8)
private String password;
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserName(String userName) {
this.userName = userName;
}
}

Categories

Resources