generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add solution, tests, modified README.md
- Loading branch information
1 parent
0d0097c
commit 36e745b
Showing
14 changed files
with
382 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package mate.academy.spring; | ||
|
||
public class Main { | ||
public static void main(String[] args) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package mate.academy.spring.config; | ||
|
||
import java.util.Properties; | ||
import javax.sql.DataSource; | ||
import org.apache.commons.dbcp2.BasicDataSource; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.core.env.Environment; | ||
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; | ||
|
||
@Configuration | ||
@PropertySource("classpath:application.properties") | ||
@ComponentScan(basePackages = "mate.academy.spring") | ||
public class AppConfig { | ||
@Autowired | ||
private Environment environment; | ||
|
||
@Bean | ||
public DataSource getDataSource() { | ||
BasicDataSource dataSource = new BasicDataSource(); | ||
dataSource.setDriverClassName(environment.getProperty("db.driver")); | ||
dataSource.setUrl(environment.getProperty("db.url")); | ||
dataSource.setUsername(environment.getProperty("db.username")); | ||
dataSource.setPassword(environment.getProperty("db.password")); | ||
return dataSource; | ||
} | ||
|
||
@Bean | ||
public LocalSessionFactoryBean getSessionFactory() { | ||
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean(); | ||
localSessionFactoryBean.setDataSource(getDataSource()); | ||
|
||
Properties properties = new Properties(); | ||
properties.put("show_sql", environment.getProperty("hibernate.show_sql")); | ||
properties.put("hibernate.hbm2ddl.auto", | ||
environment.getProperty("hibernate.hbm2_ddl.auto")); | ||
properties.put("hibernate.dialect", | ||
environment.getProperty("spring.jpa.properties.hibernate.dialect")); | ||
|
||
localSessionFactoryBean.setHibernateProperties(properties); | ||
localSessionFactoryBean.setPackagesToScan("mate.academy.spring.model"); | ||
return localSessionFactoryBean; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.spring.dao; | ||
|
||
import java.util.List; | ||
import mate.academy.spring.model.User; | ||
|
||
public interface UserDao { | ||
User add(User user); | ||
|
||
List<User> getAll(); | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/mate/academy/spring/dao/impl/UserDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package mate.academy.spring.dao.impl; | ||
|
||
import java.util.List; | ||
import mate.academy.spring.dao.UserDao; | ||
import mate.academy.spring.exception.DataProcessingException; | ||
import mate.academy.spring.model.User; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.query.Query; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class UserDaoImpl implements UserDao { | ||
|
||
@Autowired | ||
private SessionFactory sessionFactory; | ||
|
||
@Override | ||
public User add(User user) { | ||
Transaction transaction = null; | ||
Session session = null; | ||
try { | ||
session = sessionFactory.openSession(); | ||
transaction = session.beginTransaction(); | ||
session.save(user); | ||
transaction.commit(); | ||
return user; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't insert an user: " + user, e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public List<User> getAll() { | ||
try (Session session = sessionFactory.openSession()) { | ||
Query<User> query = session.createQuery("FROM User", User.class); | ||
return query.getResultList(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get all users", e); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/spring/exception/DataProcessingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.spring.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package mate.academy.spring.model; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import java.util.Objects; | ||
|
||
@Getter | ||
@Setter | ||
@ToString | ||
@Entity | ||
@Table(name = "user") | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
private String name; | ||
private int age; | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
User user = (User) o; | ||
return age == user.age && Objects.equals(id, user.id) && name.equals(user.name); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id, name, age); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/spring/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.spring.service; | ||
|
||
import java.util.List; | ||
import mate.academy.spring.model.User; | ||
|
||
public interface UserService { | ||
User add(User user); | ||
|
||
List<User> getAll(); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/mate/academy/spring/service/impl/UserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package mate.academy.spring.service.impl; | ||
|
||
import java.util.List; | ||
import mate.academy.spring.dao.UserDao; | ||
import mate.academy.spring.model.User; | ||
import mate.academy.spring.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
@Autowired | ||
private UserDao userDao; | ||
|
||
@Override | ||
public User add(User user) { | ||
return userDao.add(user); | ||
} | ||
|
||
@Override | ||
public List<User> getAll() { | ||
return userDao.getAll(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
db.driver=com.mysql.cj.jdbc.Driver | ||
db.url=jdbc:mysql://localhost:3306/spring_app?serverTimezone=UTC | ||
db.username=root | ||
db.password=root | ||
|
||
hibernate.show_sql=true | ||
hibernate.hbm2_ddl.auto=create-drop | ||
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package mate.academy.spring.dao; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import mate.academy.spring.config.AppConfig; | ||
import mate.academy.spring.model.User; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = AppConfig.class) | ||
@TestPropertySource(locations="classpath:application.properties") | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) | ||
public class UserDaoTest { | ||
private static UserDao userDao; | ||
|
||
@BeforeClass | ||
public static void setUp() { | ||
AnnotationConfigApplicationContext context = | ||
new AnnotationConfigApplicationContext(AppConfig.class); | ||
userDao = context.getBean(UserDao.class); | ||
} | ||
|
||
@Test | ||
public void add_Ok() { | ||
User user = createUser("John", 18); | ||
assertNotNull(user.getId()); | ||
long expected = 1L; | ||
long actual = user.getId(); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void getAll_NoUsers_Ok() { | ||
List<User> expected = Collections.emptyList(); | ||
List<User> actual = userDao.getAll(); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
@Test | ||
public void getAll_WithUsers_Ok() { | ||
User john = createUser("John", 18); | ||
User ann = createUser("Ann", 20); | ||
List<User> expected = List.of(john, ann); | ||
List<User> actual = userDao.getAll(); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
private User createUser(String name, int age) { | ||
User user = new User(); | ||
user.setName(name); | ||
user.setAge(age); | ||
return userDao.add(user); | ||
} | ||
} |
Oops, something went wrong.