parent
c664335349
commit
a21b428075
@ -1,7 +1,13 @@ |
|||||||
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; |
package de.kreth.vaadin.clubhelper.vaadinclubhelper.dao; |
||||||
|
|
||||||
|
import javax.persistence.EntityManager; |
||||||
|
|
||||||
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; |
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; |
||||||
|
|
||||||
public interface ClubEventDao extends IDao<ClubEvent> { |
public interface ClubEventDao extends IDao<ClubEvent> { |
||||||
|
|
||||||
|
void setEntityManager(EntityManager em); |
||||||
|
|
||||||
|
EntityManager getEntityManager(); |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,125 @@ |
|||||||
|
package de.kreth.vaadin.clubhelper.vaadinclubhelper.business; |
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.security.GeneralSecurityException; |
||||||
|
import java.util.HashSet; |
||||||
|
import java.util.List; |
||||||
|
import java.util.function.Consumer; |
||||||
|
|
||||||
|
import javax.persistence.EntityManager; |
||||||
|
|
||||||
|
import org.hibernate.Session; |
||||||
|
import org.hibernate.SessionFactory; |
||||||
|
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
import org.junit.jupiter.api.extension.ExtendWith; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.context.annotation.Bean; |
||||||
|
import org.springframework.context.annotation.Configuration; |
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension; |
||||||
|
|
||||||
|
import de.kreth.clubhelperbackend.google.calendar.CalendarAdapter; |
||||||
|
import de.kreth.vaadin.clubhelper.HibernateHolder; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.AbstractDatabaseTest; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.dao.ClubEventDaoImpl; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.ClubEvent; |
||||||
|
import de.kreth.vaadin.clubhelper.vaadinclubhelper.data.Person; |
||||||
|
|
||||||
|
@ExtendWith(SpringExtension.class) |
||||||
|
class EventBusinessTest { |
||||||
|
|
||||||
|
EventBusiness eventBusiness; |
||||||
|
|
||||||
|
private List<Person> persons; |
||||||
|
private ClubEvent event; |
||||||
|
private DatabaseHelper helper; |
||||||
|
|
||||||
|
@Autowired |
||||||
|
protected EntityManager entityManager; |
||||||
|
|
||||||
|
private EventBusiness business; |
||||||
|
|
||||||
|
@Configuration |
||||||
|
public static class MyConfig { |
||||||
|
|
||||||
|
@Bean |
||||||
|
public EntityManager getEntityManager() throws Exception { |
||||||
|
|
||||||
|
// setup the session factory
|
||||||
|
org.hibernate.cfg.Configuration configuration = HibernateHolder.configuration(); |
||||||
|
|
||||||
|
SessionFactory sessionFactory = configuration.buildSessionFactory(); |
||||||
|
return sessionFactory.openSession(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Bean |
||||||
|
public CalendarAdapter getCalendarAdapter() throws GeneralSecurityException, IOException { |
||||||
|
return new CalendarAdapter(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@BeforeEach |
||||||
|
void setUp() throws Exception { |
||||||
|
helper = new DatabaseHelper(entityManager); |
||||||
|
helper.cleanH2Database(); |
||||||
|
|
||||||
|
ClubEventDaoImpl dao = new ClubEventDaoImpl(); |
||||||
|
dao.setEntityManager(entityManager); |
||||||
|
|
||||||
|
business = new EventBusiness(); |
||||||
|
business.dao = dao; |
||||||
|
insertTestData(); |
||||||
|
business.setSelected(event); |
||||||
|
} |
||||||
|
|
||||||
|
private void insertTestData() { |
||||||
|
persons = helper.insertPersons(3); |
||||||
|
event = helper.creteEvent(); |
||||||
|
helper.transactional((session) -> session.save(event)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testDataCorrectlyCreated() { |
||||||
|
assertEquals(0, helper.loadEventPersons().size()); |
||||||
|
|
||||||
|
List<Person> stored = entityManager.createNamedQuery(Person.QUERY_FINDALL, Person.class).getResultList(); |
||||||
|
assertEquals(3, stored.size()); |
||||||
|
|
||||||
|
List<ClubEvent> events = business.loadEvents(); |
||||||
|
assertEquals(1, events.size()); |
||||||
|
// assertNotNull(events.get(0).getPersons());
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
void testAddPersonsToEvent() { |
||||||
|
helper.transactional(() -> business.changePersons(new HashSet<>(persons.subList(0, 1)))); |
||||||
|
helper.transactional(() -> business.changePersons(new HashSet<>(persons.subList(0, 2)))); |
||||||
|
|
||||||
|
assertEquals(2, helper.loadEventPersons().size()); |
||||||
|
} |
||||||
|
|
||||||
|
class DatabaseHelper extends AbstractDatabaseTest { |
||||||
|
public DatabaseHelper(EntityManager em) { |
||||||
|
this((Session) em); |
||||||
|
} |
||||||
|
|
||||||
|
public DatabaseHelper(Session session) { |
||||||
|
AbstractDatabaseTest.session = session; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void transactional(Runnable r) { |
||||||
|
super.transactional(r); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void transactional(Consumer<Session> r) { |
||||||
|
super.transactional(r); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,17 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||||
|
<launchConfiguration type="org.eclipse.m2e.Maven2LaunchConfigurationType"> |
||||||
|
<booleanAttribute key="M2_DEBUG_OUTPUT" value="false"/> |
||||||
|
<stringAttribute key="M2_GOALS" value="clean package sonar:sonar"/> |
||||||
|
<booleanAttribute key="M2_NON_RECURSIVE" value="false"/> |
||||||
|
<booleanAttribute key="M2_OFFLINE" value="false"/> |
||||||
|
<stringAttribute key="M2_PROFILES" value=""/> |
||||||
|
<listAttribute key="M2_PROPERTIES"/> |
||||||
|
<stringAttribute key="M2_RUNTIME" value="apache-maven"/> |
||||||
|
<booleanAttribute key="M2_SKIP_TESTS" value="false"/> |
||||||
|
<intAttribute key="M2_THREADS" value="1"/> |
||||||
|
<booleanAttribute key="M2_UPDATE_SNAPSHOTS" value="false"/> |
||||||
|
<stringAttribute key="M2_USER_SETTINGS" value=""/> |
||||||
|
<booleanAttribute key="M2_WORKSPACE_RESOLUTION" value="false"/> |
||||||
|
<booleanAttribute key="org.eclipse.jdt.launching.ATTR_USE_CLASSPATH_ONLY_JAR" value="false"/> |
||||||
|
<stringAttribute key="org.eclipse.jdt.launching.WORKING_DIRECTORY" value="${project_loc:vaadin-clubhelper}"/> |
||||||
|
</launchConfiguration> |
||||||
Loading…
Reference in new issue