Search in sources :

Example 1 with User

use of com.stackify.models.User in project tutorials by eugenp.

the class DynamicTests method dynamicUserTestCollection.

@TestFactory
public Stream<DynamicTest> dynamicUserTestCollection() {
    List<User> inputList = Arrays.asList(new User("john@yahoo.com", "John"), new User("ana@yahoo.com", "Ana"));
    Function<User, String> displayNameGenerator = (input) -> "Saving user: " + input;
    UserDAO userDAO = new UserDAO();
    ThrowingConsumer<User> testExecutor = (input) -> {
        userDAO.add(input);
        assertNotNull(userDAO.findOne(input.getEmail()));
    };
    return DynamicTest.stream(inputList.iterator(), displayNameGenerator, testExecutor);
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Stream(java.util.stream.Stream) TestFactory(org.junit.jupiter.api.TestFactory) Collection(java.util.Collection) UserDAO(com.stackify.daos.UserDAO) Assertions(org.junit.jupiter.api.Assertions) DynamicTest(org.junit.jupiter.api.DynamicTest) User(com.stackify.models.User) Function(java.util.function.Function) ThrowingConsumer(org.junit.jupiter.api.function.ThrowingConsumer) User(com.stackify.models.User) UserDAO(com.stackify.daos.UserDAO) TestFactory(org.junit.jupiter.api.TestFactory)

Example 2 with User

use of com.stackify.models.User in project tutorials by eugenp.

the class UserDAO method findAll.

public List<User> findAll() {
    List<User> users = new ArrayList<>();
    try (Connection con = ConnectionUtil.getConnection()) {
        String query = "SELECT * FROM users";
        PreparedStatement pstmt = con.prepareStatement(query);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            User user = new User();
            user.setEmail(rs.getString("email"));
            user.setName(rs.getString("name"));
            users.add(user);
        }
    } catch (SQLException exc) {
        logger.error(exc.getMessage());
    }
    return users;
}
Also used : User(com.stackify.models.User) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 3 with User

use of com.stackify.models.User in project tutorials by eugenp.

the class UserDAO method findOne.

public User findOne(String email) {
    User user = null;
    try (Connection con = ConnectionUtil.getConnection()) {
        String query = "SELECT * FROM users WHERE email=?";
        PreparedStatement pstmt = con.prepareStatement(query);
        pstmt.setString(1, email);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            user = new User();
            user.setEmail(rs.getString("email"));
            user.setName(rs.getString("name"));
        }
    } catch (SQLException exc) {
        logger.error(exc.getMessage());
    }
    return user;
}
Also used : User(com.stackify.models.User) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 4 with User

use of com.stackify.models.User in project tutorials by eugenp.

the class MyServiceTest method testService.

@Test
public void testService() {
    MyService myService = new MyService();
    User user = new User("John", "john@yahoo.com");
    user.setDateOfBirth(LocalDate.of(1980, Month.APRIL, 20));
    logger.info("Age of user {} is {}", () -> user.getName(), () -> myService.calculateUserAge(user));
}
Also used : User(com.stackify.models.User) MyService(com.stackify.services.MyService) Test(org.junit.Test)

Aggregations

User (com.stackify.models.User)4 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 UserDAO (com.stackify.daos.UserDAO)1 MyService (com.stackify.services.MyService)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 List (java.util.List)1 Function (java.util.function.Function)1 Stream (java.util.stream.Stream)1 Test (org.junit.Test)1 Assertions (org.junit.jupiter.api.Assertions)1 DynamicTest (org.junit.jupiter.api.DynamicTest)1 TestFactory (org.junit.jupiter.api.TestFactory)1 ThrowingConsumer (org.junit.jupiter.api.function.ThrowingConsumer)1