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