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.tasksJsf;
18  
19  import static org.junit.Assert.assertNull;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.io.FileNotFoundException;
23  
24  import javax.inject.Inject;
25  import javax.persistence.EntityManager;
26  
27  import org.jboss.arquillian.container.test.api.Deployment;
28  import org.jboss.arquillian.junit.Arquillian;
29  import org.jboss.as.quickstarts.tasksJsf.Resources;
30  import org.jboss.as.quickstarts.tasksJsf.Task;
31  import org.jboss.as.quickstarts.tasksJsf.TaskDao;
32  import org.jboss.as.quickstarts.tasksJsf.User;
33  import org.jboss.as.quickstarts.tasksJsf.UserDao;
34  import org.jboss.as.quickstarts.tasksJsf.UserDaoImpl;
35  import org.jboss.shrinkwrap.api.spec.WebArchive;
36  import org.junit.Assert;
37  import org.junit.Test;
38  import org.junit.runner.RunWith;
39  
40  /**
41   * @author Lukas Fryc
42   * @author Oliver Kiss
43   */
44  @RunWith(Arquillian.class)
45  public class UserDaoTest {
46  
47      @Deployment
48      public static WebArchive deployment() throws IllegalArgumentException, FileNotFoundException {
49          return new DefaultDeployment().withPersistence().withImportedData().getArchive()
50                  .addClasses(Resources.class, User.class, UserDao.class, Task.class, TaskDao.class, UserDaoImpl.class);
51      }
52  
53      @Inject
54      private UserDao userDao;
55  
56      @Inject
57      private EntityManager em;
58  
59      @Test
60      public void userDao_should_create_user_so_it_could_be_retrieved_from_userDao_by_username() {
61          // given
62          User created = new User("username1");
63  
64          // when
65          userDao.createUser(created);
66          User retrieved = userDao.getForUsername("username1");
67  
68          // then
69          assertTrue(em.contains(created));
70          assertTrue(em.contains(retrieved));
71          Assert.assertEquals(created, retrieved);
72      }
73  
74      @Test
75      public void user_should_be_retrievable_from_userDao_by_username() {
76          // given
77          String username = "jdoe";
78  
79          // when
80          User retrieved = userDao.getForUsername(username);
81  
82          // then
83          Assert.assertEquals(username, retrieved.getUsername());
84      }
85  
86      @Test
87      public void userDao_should_return_null_when_searching_for_non_existent_user() {
88          // given
89          String nonExistent = "nonExistent";
90  
91          // when
92          User retrieved = userDao.getForUsername(nonExistent);
93  
94          // then
95          assertNull(retrieved);
96      }
97  }