unit tests for feed bean
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / FeedTest.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.json.JSONObject;
26 import org.junit.*;
27 import org.junit.runner.RunWith;
28 import org.onap.dmaap.datarouter.provisioning.utils.DB;
29 import org.powermock.modules.junit4.PowerMockRunner;
30
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.Persistence;
34 import java.io.InvalidObjectException;
35 import java.sql.SQLException;
36 import java.util.List;
37
38 @RunWith(PowerMockRunner.class)
39 public class FeedTest {
40     private static EntityManagerFactory emf;
41     private static EntityManager em;
42     private Feed feed;
43     private DB db;
44
45     @BeforeClass
46     public static void init() {
47         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
48         em = emf.createEntityManager();
49         System.setProperty(
50                 "org.onap.dmaap.datarouter.provserver.properties",
51                 "src/test/resources/h2Database.properties");
52     }
53
54     @AfterClass
55     public static void tearDownClass() {
56         em.clear();
57         em.close();
58         emf.close();
59     }
60
61     @Before
62     public void setUp() throws Exception {
63         db = new DB();
64         feed = new Feed("Feed1","v0.1", "First Feed for testing", "First Feed for testing");
65         feed.setFeedid(1);
66         feed.setGroupid(1);
67         feed.setPublisher("pub");
68         feed.setDeleted(false);
69     }
70
71     @Test
72     public void Given_getFilteredFeedUrlList_With_Name_Then_Method_Returns_Self_Links() {
73         List<String>  list= feed.getFilteredFeedUrlList("name","Feed1");
74         Assert.assertEquals("self_link",list.get(0));
75     }
76
77     @Test
78     public void Given_getFilteredFeedUrlList_With_Publ_Then_Method_Returns_Self_Links() {
79         List<String>  list= feed.getFilteredFeedUrlList("publ","pub");
80         Assert.assertEquals("self_link",list.get(0));
81     }
82
83     @Test
84     public void Given_getFilteredFeedUrlList_With_Subs_Then_Method_Returns_Self_Links() {
85         List<String>  list= feed.getFilteredFeedUrlList("subs","sub123");
86         Assert.assertEquals("self_link",list.get(0));
87     }
88
89     @Test
90     public void Given_doDelete_Succeeds_Then_doInsert_To_Put_Feed_Back_And_Bool_Is_True() throws SQLException, InvalidObjectException {
91         Boolean bool = feed.doDelete(db.getConnection());
92         Assert.assertEquals(true, bool);
93         JSONObject jo = new JSONObject();
94         jo.put("self","self_link");
95         jo.put("publish","publish_link");
96         jo.put("subscribe","subscribe_link");
97         jo.put("log","log_link");
98         feed.setLinks(new FeedLinks(jo));
99         bool = feed.doInsert(db.getConnection());
100         Assert.assertEquals(true, bool);
101     }
102
103     @Test
104     public void Validate_ChaneOwnerShip_Returns_True()
105     {
106         Boolean bool = feed.changeOwnerShip();
107         Assert.assertEquals(true, bool);
108     }
109
110     @Test
111     public void Given_Feeds_Are_Equal_Then_Equals_Returns_True()
112     {
113         Feed feed2 = feed;
114         Boolean bool = feed.equals(feed2);
115         Assert.assertEquals(true, bool);
116     }
117 }