[DMaaP DR] JKD 11 migration
[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 java.sql.Connection;
26 import java.util.Collection;
27 import java.util.List;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.Persistence;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
39 import org.powermock.core.classloader.annotations.PowerMockIgnore;
40 import org.powermock.modules.junit4.PowerMockRunner;
41
42 @RunWith(PowerMockRunner.class)
43 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
44 public class GroupTest {
45   private static EntityManagerFactory emf;
46   private static EntityManager em;
47   private Group group;
48   private ProvDbUtils provDbUtils = ProvDbUtils.getInstance();
49
50   @BeforeClass
51   public static void init() {
52     emf = Persistence.createEntityManagerFactory("dr-unit-tests");
53     em = emf.createEntityManager();
54     System.setProperty(
55         "org.onap.dmaap.datarouter.provserver.properties",
56         "src/test/resources/h2Database.properties");
57   }
58
59   @AfterClass
60   public static void tearDownClass() {
61     em.clear();
62     em.close();
63     emf.close();
64   }
65
66   @Before
67   public void setUp() throws Exception {
68     group = new Group("GroupTest", "This group has a description", "");
69     try (Connection conn = provDbUtils.getConnection()) {
70       group.doInsert(conn);
71     }
72   }
73
74   @After
75   public void tearDown() throws Exception {
76     try (Connection conn = provDbUtils.getConnection()) {
77       group.doDelete(conn);
78     }
79   }
80
81   @Test
82   public void Given_Group_Exists_In_Db_GetAllGroups_Returns_Correct_Group() {
83     Collection<Group> groups = Group.getAllgroups();
84     Assert.assertEquals("Group1", ((List<Group>) groups).get(0).getName());
85   }
86
87   @Test
88   public void Given_Group_Inserted_Into_Db_GetGroupMatching_Returns_Created_Group() {
89     Assert.assertEquals(group, Group.getGroupMatching(group));
90   }
91
92   @Test
93   public void Given_Group_Inserted_With_Same_Name_GetGroupMatching_With_Id_Returns_Correct_Group()
94       throws Exception {
95     Group sameGroupName = new Group("GroupTest", "This group has a description", "");
96     sameGroupName.doInsert(provDbUtils.getConnection());
97     Assert.assertEquals(
98         "This group has a description", Group.getGroupMatching(group, 2).getDescription());
99     sameGroupName.doDelete(provDbUtils.getConnection());
100   }
101
102   @Test
103   public void Given_Group_AuthId_Updated_GetGroupByAuthId_Returns_Correct_Group() throws Exception {
104     group.setAuthid("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9");
105     group.doUpdate(provDbUtils.getConnection());
106     Assert.assertEquals(group, Group.getGroupByAuthId("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9"));
107   }
108
109   @Test
110   public void Given_Group_Inserted_GetGroupById_Returns_Correct_Group() {
111     Assert.assertEquals(group, Group.getGroupById(group.getGroupid()));
112   }
113 }