I am writing unit Junit test case for entity java bean.But, when i call the EJB method i am getting an null pointer exception the code is like below
UserServiceTest.java
#PersistenceContext(unitName = "PU")
private EntityManager em;
private static UserService userService;
#Test
public void registerUser() {
userService = new UserService();
resultData = userService.registerUser(name, email, password, mobileNumber, countryId);
Assert.assertEquals(true, resultData.isSet);
Assert.assertEquals(message, resultData.message);
}
UserService.java
#PersistenceContext(unitName = "PU")
private EntityManager em;
public ResultData registerUser(String name, String email, String password, String mobileNumber, String countryId) {
try {
Query q = em.createNamedQuery("User.findByEmail", User.class).setParameter("email", email);
users = q.getResultList();
if (!users.isEmpty()) {
userDataClass.isSet = false;
userDataClass.message = "A user with this email already exists.";
return userDataClass;
}catch (Exception e) {
userDataClass.isSet = false;
}
The above code i debugged. When the ponter came to entityManager
Query q = em.createNamedQuery("User.findByEmail", User.class).setParameter("email", email);
I am getting an null pointer exception.
Can any one plz help me
Related
I'm using Spring Boot 2.1 with Java 11. I have annotated my User model with fasterxml annotations so that my password can be accepted for POST requests, but not returned for other REST requests ...
#Data
#Entity
#Table(name = "Users")
public class User implements UserDetails {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
private String firstName;
private String lastName;
#NotBlank(message = "Email is mandatory")
#Column(unique=true)
private String email;
#JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
private boolean enabled;
private boolean tokenExpired;
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(
name = "users_roles",
joinColumns = #JoinColumn(
name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = #JoinColumn(
name = "role_id", referencedColumnName = "id"))
private Collection<Role> roles;
#Override
public Collection<? extends GrantedAuthority> getAuthorities() {
// TODO Auto-generated method stub
return null;
}
#Override
public String getUsername() {
return email;
}
#Override
public boolean isAccountNonExpired() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isAccountNonLocked() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isCredentialsNonExpired() {
// TODO Auto-generated method stub
return false;
}
#PrePersist #PreUpdate
private void prepare(){
this.email = this.email.toLowerCase();
}
}
However, when trying to run an integration test, the password is not getting translated by "objectMapper.writeValueAsString". Here is my test ...
#SpringBootTest(classes = CardmaniaApplication.class,
webEnvironment = WebEnvironment.RANDOM_PORT)
public class UserControllerIntegrationTest {
#Autowired
private TestRestTemplate restTemplate;
#Autowired
private ObjectMapper objectMapper;
#Autowired
private IUserRepository userRepository;
#Test
#WithUserDetails("me#example.com")
void registrationWorksThroughAllLayers() throws Exception {
final String email = "newuser#test.com";
final String firstName = "first";
final String lastName = "last";
final String password = "password";
User user = getTestUser(email, password, firstName, lastName, Name.USER);
ResponseEntity<String> responseEntity = this.restTemplate
.postForEntity("http://localhost:" + port + "/api/users", user, String.class);
assertEquals(201, responseEntity.getStatusCodeValue());
final User createdUser = userRepository.findByEmail(email);
assertNotNull(createdUser);
assertNotNull(createdUser.getPassword());
}
#Test
#WithUserDetails("me#example.com")
void getDetailsAboutMyself() throws JsonProcessingException, JSONException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails user = (UserDetails) authentication.getPrincipal();
final User foundUser = userRepository.findByEmail(user.getUsername());
ResponseEntity<String> responseEntity = this.restTemplate
.getForEntity("http://localhost:" + port + "/api/users/" + foundUser.getId(), String.class);
assertEquals(200, responseEntity.getStatusCodeValue());
// assert proper response
final String userAsJson = objectMapper.writeValueAsString(user);
assertEquals(userAsJson, responseEntity.getBody());
JSONObject object = (JSONObject) new JSONTokener(userAsJson).nextValue();
// Verify no password is returned.
assertNull(object.getString("password"));
}
...
}
The JSON from the objectMapper.writeValueAsString call is
{"id":null,"firstName":"first","lastName":"last","email":"newuser#test.com","enabled":true,"tokenExpired":false,"roles":null,"username":"newuser#test.com","authorities":null,"accountNonExpired":false,"accountNonLocked":false,"credentialsNonExpired":false}
What's the proper way to get my password included as part of the mapping as well as suppressing the password when requesting my entity from read endpoints?
This is a common misunderstanding, there was even a bug report for it and they clarified the documentation.
"READ" and "WRITE" are to be understood from the perspective of the Java Property, i.e., when you serialize an Object, you have to read the property and when you deserialize it, you have to write it.
In your case, you want #JsonProperty(access = JsonProperty.Access.READ_ONLY)
Using WRITE_ONLY or READ_ONLY will not work in your case. The reason is one call over http needs both. Lets take this.restTemplate.postForEntity as an example. In the sending side, your User java object need to serialised to json so it needs the READ and when the rest endpoint receives the json, it need to deserialise the json into User java object so needs the WRITE. It will be the same scenario for this.restTemplate.getForEntity too
One solution is to set the password field of User on the GET endpoint to null before returning
Another solution is create a separate UserDto without password field and return it from GET endpoint
Another solution is to create two JsonViews where one is with password and other one is without password. Then annotate your endpoints with correct #JsonView
I'm using Gson to manage my Json file. I have a User class which looks like this:
public class User {
private StringProperty username;
private StringProperty password;
...
public User() {}
public User(String user, String pass, ...) {
this.username = new SimpleStringProperty(user);
this.password = new SimpleStringProperty(pass);
...
}
public String getUsername() {
return username.get();
}
public void setUsername(String username) {
this.username.set(username);;
}
...
}
And this is how I add a User to the Json file
public static boolean addUser(User user) throws IOException{
String users = new String(Files.readAllBytes(Paths.get("users.json")));
List<User> userList = getUsers(users);
if (userList.contains(user)) return false;
userList.add(user);
Writer writer = new FileWriter("users.json");
Gson gson = new GsonBuilder().create();
gson.toJson(userList, writer);
writer.close();
return true;
}
gson.toJson(userList, writer) is throwing this error:
Exception in thread "JavaFX Application Thread" java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.lang.Object javafx.beans.property.SimpleStringProperty.bean accessible: module javafx.base does not "opens javafx.beans.property" to unnamed module #4bf59938
I know it has something to do with the StringProperty attributes, but I don't know what's wrong.
I recently followed a tutorial on authorization and authentication (login) and everything works fine but everytime I input the username and password it's showing an error of bad credentials. This is my code.
WebSecurityConfig.java
#Configuration
#EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
#Autowired
UserDetailsServiceImpl userDetailsService;
#Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}
#Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
// Setting Service to find User in the database.
// And Setting PassswordEncoder
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}
}
UserDAO.java
#Repository
#Transactional
public class UserDAO extends JdbcDaoSupport {
#Autowired
public UserDAO(DataSource dataSource) {
this.setDataSource(dataSource);
}
public WebUser findUserAccount(String userName) {
// Select .. from App_User u Where u.User_Name = ?
String sql = WebUserMapper.BASE_SQL + " where u.Username = ? ";
Object[] params = new Object[] { userName };
WebUserMapper mapper = new WebUserMapper();
try {
WebUser userInfo = this.getJdbcTemplate().queryForObject(sql, params, mapper);
return userInfo;
} catch (EmptyResultDataAccessException e) {
return null;
}
}
}
WebUserMapper.java
public class WebUserMapper implements RowMapper<WebUser> {
public static final String BASE_SQL //
= "Select u.Id, u.Username, u.Pass From User u ";
#Override
public WebUser mapRow(ResultSet rs, int rowNum) throws SQLException {
Long userId = rs.getLong("Id");
String userName = rs.getString("Username");
String encrytedPassword = rs.getString("Pass");
return new WebUser(userId, userName, encrytedPassword);
}
}
EncryptedPasswordUtils.java
public class EncrytedPasswordUtils {
public static String encrytePassword(String password) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
return encoder.encode(password);
}
}
this is my database:
Error:
Found User: test/$2y$12$nUaUkOYaz0hAsgSippzpdeYPPqFsx3zKT9/H5vojD.YBBlZhLo4RG
2018-10-22 09:53:20.054 WARN 11316 --- [nio-8080-exec-6] o.s.s.c.bcrypt.BCryptPasswordEncoder : Encoded password does not look like BCrypt
Reall hope you could help me. The username can be found and it's showing an error everytime I tried a username and password that is not in the database. Even though I input the right password which is "123" it's returning bad credentials. Thank you so much
Im very new to the Spring boot. according to the client requirement my other member developed code segment using spring boot to get user response from here. Here is the code segment.
public GenericResponse updateAcceptOrReject(String password, FileAcceptStatus status) {
// TODO Auto-generated method stub
GenericResponse genericResponse = new GenericResponse();
String sql = "SELECT * FROM employee WHERE pass_code = ?";
Employee employee = null;
try {
employee = (Employee) jdbcTemplate.queryForObject(sql, new Object[] { password }, new EmployeeRowMapper());
} catch (EmptyResultDataAccessException e) {
System.out.println("error :"+ e.getLocalizedMessage());
employee = null;
}catch (Exception e) {
System.out.println("error :"+ e.getLocalizedMessage());
employee = null;
}
// check if employee available
if(employee == null) {
genericResponse.setStatusCode(200);
genericResponse.setMessage("No employee found");
return genericResponse;
}
// check acceptStatus
if(employee.getAccept_status() != 0) {
genericResponse.setStatusCode(201);
genericResponse.setMessage("You already accepted");
return genericResponse;
}
String updateSql = "update employee set accept_status=? where pass_code=?";
int[] types = {Types.INTEGER, Types.VARCHAR};
int updatedFlag = jdbcTemplate.update(updateSql, new Object[] { status.ordinal(), password }, types);
if(updatedFlag == 1) {
genericResponse.setStatusCode(0);
genericResponse.setMessage("Success");
return genericResponse;
}else {
genericResponse.setStatusCode(-99);
genericResponse.setMessage("Error occured while updating employ");
return genericResponse;
}
}
what i want is load separate static HTML page with a Success message located in another package rather that stating Success message in genericResponse.setMessage(). likewise static pages for other messages too.
Can anyone help me here?
Controller is like
#RestController
public class HumanResourceController {
#Autowired
private HumanResourceService hRService;
#RequestMapping(value = "/update/status/{password:.+}/{status}", method = RequestMethod.GET)
public ResponseEntity<?> updateAcceptOrReject(#PathVariable("password") String password,
#PathVariable("status") FileAcceptStatus status) {
GenericResponse genericResponse = hRService.updateAcceptOrReject(password, status);
return ResponseEntity.ok().body(genericResponse);
}
}
The #RestController will always return json message, so you should change it to #Controller, then you need to create a viewresolver to render the static html
move all your html file into /WEB-INF/
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".html");
return resolver;
}
#Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
#Controller
public class HumanResourceController {
#Autowired
private HumanResourceService hRService;
#RequestMapping(value = "/update/status/{password:.+}/{status}", method = RequestMethod.GET)
public String updateAcceptOrReject(#PathVariable("password") String password,
#PathVariable("status") FileAcceptStatus status) {
GenericResponse genericResponse = hRService.updateAcceptOrReject(password, status);
// you can return various view according the generic response http code
return "alreadyaccepted";
}
}
I am trying to run spark job locally to read mysql table contents (in local machine) to jdbcRDD. From online i gathered the below source code and customised to read element table and load all the columns.
private static final JavaSparkContext sc = new JavaSparkContext(
new SparkConf().setAppName("SparkJdbc").setMaster("local[*]"));
private static final String MYSQL_DRIVER = "com.mysql.jdbc.Driver";
private static final String MYSQL_CONNECTION_URL = "jdbc:mysql://localhost:3306/ddm";
private static final String MYSQL_USERNAME = "root";
private static final String MYSQL_PWD = "root";
public static void main(String[] args) {
DbConnection dbConnection = new DbConnection(MYSQL_DRIVER,
MYSQL_CONNECTION_URL, MYSQL_USERNAME, MYSQL_PWD);
// Load data from MySQL
JdbcRDD<Object[]> jdbcRDD = new JdbcRDD<>(sc.sc(), dbConnection,
"select * from element where elementid >= ? and elementid <= ?",
1000, 1100, 10, new MapResult(),
ClassManifestFactory$.MODULE$.fromClass(Object[].class));
// Convert to JavaRDD
JavaRDD<Object[]> javaRDD = JavaRDD.fromRDD(jdbcRDD,
ClassManifestFactory$.MODULE$.fromClass(Object[].class));
// Join first name and last name
List<String> employeeFullNameList = javaRDD.map(
new Function<Object[], String>() {
private static final long serialVersionUID = 1L;
#Override
public String call(final Object[] record) throws Exception {
return record[2] + " " + record[3];
}
}).collect();
for (String fullName : employeeFullNameList) {
System.out.println(fullName);
}
}
static class DbConnection extends AbstractFunction0<Connection> implements
Serializable {
private static final long serialVersionUID = 1L;
private String driverClassName;
private String connectionUrl;
private String userName;
private String password;
public DbConnection(String driverClassName, String connectionUrl,
String userName, String password) {
this.driverClassName = driverClassName;
this.connectionUrl = connectionUrl;
this.userName = userName;
this.password = password;
}
#Override
public Connection apply() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
}
Properties properties = new Properties();
properties.setProperty("user", userName);
properties.setProperty("password", password);
Connection connection = null;
try {
connection = DriverManager.getConnection(connectionUrl,
properties);
} catch (SQLException e) {
}
return connection;
}
}
static class MapResult extends AbstractFunction1<ResultSet, Object[]>
implements Serializable {
private static final long serialVersionUID = 1L;
public Object[] apply(ResultSet row) {
return JdbcRDD.resultSetToObjectArray(row);
}
}
However, when I execute the code i get below NullPointerException.
15/08/01 08:27:23 ERROR Executor: Exception in task 0.0 in stage 0.0 (TID 0)java.lang.NullPointerException
at org.apache.spark.rdd.JdbcRDD$$anon$1.<init>(JdbcRDD.scala:79)
at org.apache.spark.rdd.JdbcRDD.compute(JdbcRDD.scala:74)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
at org.apache.spark.rdd.MapPartitionsRDD.compute(MapPartitionsRDD.scala:35)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:277)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:244)
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:63)
at org.apache.spark.scheduler.Task.run(Task.scala:70)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:213)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
I have looked up the jdbcrdd.scala code in github and at line 79 it points to SQL stmt.
val stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)
So, the above statement is failing. I have given the required details, but it is throwing null exception. Can anyone help me where i am doing wrong?
I have overlooked my import statements. After i have added below code I am able to execute the spark program locally.
import java.sql.{PreparedStatement, Connection, ResultSet}