unit tests for feed bean
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / GroupTest.java
1 /*******************************************************************************
2  * ============LICENSE_START==================================================
3  * * org.onap.dmaap
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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  * *
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * *
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  * *
22  ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning.beans;
24
25 import org.junit.*;
26 import org.junit.runner.RunWith;
27 import org.onap.dmaap.datarouter.provisioning.utils.DB;
28 import org.powermock.modules.junit4.PowerMockRunner;
29
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.Persistence;
33 import java.util.Collection;
34 import java.util.Date;
35 import java.util.List;
36
37 @RunWith(PowerMockRunner.class)
38 public class GroupTest {
39   private static EntityManagerFactory emf;
40   private static EntityManager em;
41   private Group group;
42   private DB db;
43
44   @BeforeClass
45   public static void init() {
46     emf = Persistence.createEntityManagerFactory("dr-unit-tests");
47     em = emf.createEntityManager();
48     System.setProperty(
49         "org.onap.dmaap.datarouter.provserver.properties",
50         "src/test/resources/h2Database.properties");
51   }
52
53   @AfterClass
54   public static void tearDownClass() {
55     em.clear();
56     em.close();
57     emf.close();
58   }
59
60   @Before
61   public void setUp() throws Exception {
62     db = new DB();
63     group = new Group("GroupTest", "", "");
64     group.doInsert(db.getConnection());
65   }
66
67   @Test
68   public void Given_Group_Exists_In_Db_GetAllGroups_Returns_Correct_Group() {
69     Collection<Group> groups = Group.getAllgroups();
70     Assert.assertEquals("Group1", ((List<Group>) groups).get(0).getName());
71   }
72
73   @Test
74   public void Given_Group_Inserted_Into_Db_GetGroupMatching_Returns_Created_Group() {
75     Assert.assertEquals(group, Group.getGroupMatching(group));
76   }
77
78   @Test
79   public void Given_Group_Inserted_With_Same_Name_GetGroupMatching_With_Id_Returns_Correct_Group()
80       throws Exception {
81     Group sameGroupName = new Group("GroupTest", "This group has a description", "");
82     sameGroupName.doInsert(db.getConnection());
83     Assert.assertEquals(
84         "This group has a description", Group.getGroupMatching(group, 2).getDescription());
85     sameGroupName.doDelete(db.getConnection());
86   }
87
88   @Test
89   public void Given_Group_Inserted_GetGroupById_Returns_Correct_Group() {
90     Assert.assertEquals(group, Group.getGroupById(group.getGroupid()));
91   }
92
93   @Test
94   public void Given_Group_AuthId_Updated_GetGroupByAuthId_Returns_Correct_Group() throws Exception {
95     group.setAuthid("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9");
96     group.doUpdate(db.getConnection());
97     Assert.assertEquals(group, Group.getGroupByAuthId("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9"));
98   }
99
100   @After
101   public void tearDown() throws Exception {
102     group.doDelete(db.getConnection());
103   }
104 }