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