View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.tasks;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.FileNotFoundException;
23  import java.util.List;
24  
25  import javax.inject.Inject;
26  import javax.persistence.EntityManager;
27  
28  import org.jboss.arquillian.container.test.api.Deployment;
29  import org.jboss.arquillian.junit.Arquillian;
30  import org.jboss.arquillian.junit.InSequence;
31  import org.jboss.shrinkwrap.api.spec.WebArchive;
32  import org.junit.Before;
33  import org.junit.Test;
34  import org.junit.runner.RunWith;
35  
36  /**
37   * @author Lukas Fryc
38   * @author Oliver Kiss
39   */
40  @RunWith(Arquillian.class)
41  public class TaskDaoTest {
42  
43      @Deployment
44      public static WebArchive deployment() throws IllegalArgumentException, FileNotFoundException {
45          return DefaultDeployment.deployment();
46      }
47  
48      @Inject
49      EntityManager em;
50  
51      @Inject
52      TaskDao taskDao;
53  
54      private User detachedUser;
55  
56      @Before
57      public void setUp() throws Exception {
58          detachedUser = new User("jdoe");
59          detachedUser.setId(1L);
60      }
61  
62      @Test
63      @InSequence(1)
64      public void user_should_be_created_with_one_task_attached() throws Exception {
65          // given
66          User user = new User("New user");
67          Task task = new Task("New task");
68  
69          // when
70          em.persist(user);
71          taskDao.createTask(user, task);
72          List<Task> userTasks = em.createQuery("SELECT t FROM Task t WHERE t.owner = :owner", Task.class).setParameter("owner", user)
73                  .getResultList();
74  
75          // then
76          assertEquals(1, userTasks.size());
77          assertEquals(task, userTasks.get(0));
78      }
79  
80      @Test
81      @InSequence(2)
82      public void all_tasks_should_be_obtained_from_detachedUser() {
83          // when
84          List<Task> userTasks = taskDao.getAll(detachedUser);
85  
86          // then
87          assertEquals(2, userTasks.size());
88      }
89  
90      @Test
91      @InSequence(3)
92      public void range_of_tasks_should_be_provided_by_taskDao() {
93          // when
94          List<Task> headOfTasks = taskDao.getRange(detachedUser, 0, 1);
95          List<Task> tailOfTasks = taskDao.getRange(detachedUser, 1, 1);
96  
97          // then
98          assertEquals(1, headOfTasks.size());
99          assertEquals(1, tailOfTasks.size());
100         assertTrue(headOfTasks.get(0).getTitle().contains("first"));
101         assertTrue(tailOfTasks.get(0).getTitle().contains("second"));
102     }
103 
104     @Test
105     @InSequence(4)
106     public void taskDao_should_provide_basic_case_insensitive_full_text_search() {
107         // given
108         String taskTitlePart = "FIRST";
109 
110         // when
111         List<Task> titledTasks = taskDao.getForTitle(detachedUser, taskTitlePart);
112 
113         // then
114         assertEquals(1, titledTasks.size());
115         assertTrue(titledTasks.get(0).getTitle().contains("first"));
116     }
117 
118     @Test
119     @InSequence(5)
120     public void taskDao_should_remove_task_from_detachedUser() {
121         // given
122         Task task = new Task();
123         task.setId(1L);
124         task.setOwner(detachedUser);
125         assertEquals(2, taskDao.getAll(detachedUser).size());
126 
127         // when
128         taskDao.deleteTask(task);
129 
130         // then
131         assertEquals(1, taskDao.getAll(detachedUser).size());
132     }
133 }