update link to upper-constraints.txt
[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 static org.mockito.Matchers.anyString;
26
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32 import javax.persistence.EntityManager;
33 import javax.persistence.EntityManagerFactory;
34 import javax.persistence.Persistence;
35 import org.json.JSONObject;
36 import org.junit.AfterClass;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mockito;
43 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
44 import org.powermock.core.classloader.annotations.PowerMockIgnore;
45 import org.powermock.modules.junit4.PowerMockRunner;
46
47 @RunWith(PowerMockRunner.class)
48 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
49 public class FeedTest {
50     private static EntityManagerFactory emf;
51     private static EntityManager em;
52     private Feed feed;
53     private ProvDbUtils provDbUtils;
54
55     @BeforeClass
56     public static void init() {
57         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
58         em = emf.createEntityManager();
59         System.setProperty(
60                 "org.onap.dmaap.datarouter.provserver.properties",
61                 "src/test/resources/h2Database.properties");
62     }
63
64     @AfterClass
65     public static void tearDownClass() {
66         em.clear();
67         em.close();
68         emf.close();
69     }
70
71     @Before
72     public void setUp() throws Exception {
73         provDbUtils = ProvDbUtils.getInstance();
74         feed = new Feed("Feed1","v0.1", "First Feed for testing", "First Feed for testing");
75         feed.setFeedid(1);
76         feed.setGroupid(1);
77         feed.setPublisher("pub");
78         feed.setDeleted(false);
79     }
80
81     @Test
82     public void Given_getFilteredFeedUrlList_With_Name_Then_Method_Returns_Self_Links() {
83         List<String>  list= Feed.getFilteredFeedUrlList("name","Feed1");
84         Assert.assertEquals("self_link",list.get(0));
85     }
86
87     @Test
88     public void Given_getFilteredFeedUrlList_With_Publ_Then_Method_Returns_Self_Links() {
89         List<String>  list= Feed.getFilteredFeedUrlList("publ","pub");
90         Assert.assertEquals("self_link",list.get(0));
91     }
92
93     @Test
94     public void Given_getFilteredFeedUrlList_With_Subs_Then_Method_Returns_Self_Links() {
95         List<String>  list= Feed.getFilteredFeedUrlList("subs","sub123");
96         Assert.assertEquals("self_link",list.get(0));
97     }
98
99     @Test
100     public void Given_doDelete_Succeeds_Then_doInsert_To_Put_Feed_Back_And_Bool_Is_True() throws SQLException {
101         boolean bool = feed.doDelete(provDbUtils.getConnection());
102         Assert.assertTrue(bool);
103         JSONObject jo = new JSONObject();
104         jo.put("self","self_link");
105         jo.put("publish","publish_link");
106         jo.put("subscribe","subscribe_link");
107         jo.put("log","log_link");
108         feed.setLinks(new FeedLinks(jo));
109         bool = feed.doInsert(provDbUtils.getConnection());
110         Assert.assertTrue(bool);
111     }
112
113     @Test
114     public void Validate_ChaneOwnerShip_Returns_True() {
115         Boolean bool = feed.changeOwnerShip();
116         Assert.assertEquals(true, bool);
117     }
118
119     @Test
120     public void Given_Feeds_Are_Equal_Then_Equals_Returns_True() {
121         Feed feed2 = feed;
122         Boolean bool = feed.equals(feed2);
123         Assert.assertEquals(true, bool);
124     }
125
126     @Test
127     public void Validate_getFeedByNameVersion_Returns_Valid_Feed() {
128         feed = Feed.getFeedByNameVersion("Feed1","v0.1");
129         Assert.assertEquals(feed.toString(), "FEED: feedid=1, name=Feed1, version=v0.1");
130     }
131
132     @Test
133     public void Given_doDelete_Throws_SQLException_Then_Returns_False() throws SQLException {
134         Connection spyConnection = CreateSpyForDbConnection();
135         Mockito.doThrow(new SQLException()).when(spyConnection).prepareStatement(anyString());
136         Assert.assertFalse(feed.doDelete(spyConnection));
137     }
138
139     @Test
140     public void Given_doInsert_Throws_SQLException_Then_Returns_False() throws SQLException {
141         Connection connection = provDbUtils.getConnection();
142         FeedAuthorization fa = new FeedAuthorization();
143         Set setA = new HashSet();
144         setA.add(new FeedEndpointID("1", "Name"));
145         Set setB = new HashSet();
146         setB.add("172.0.0.1");
147         fa.setEndpointIDS(setA);
148         fa.setEndpointAddrs(setB);
149         feed.setAuthorization(fa);
150         Assert.assertFalse(feed.doInsert(connection));
151
152     }
153
154     @Test
155     public void Given_doUpdate_Throws_SQLException_Then_Returns_False() throws SQLException {
156         Connection spyConnection = CreateSpyForDbConnection();
157         Mockito.doThrow(new SQLException()).when(spyConnection).prepareStatement(anyString());
158         Assert.assertFalse(feed.doUpdate(spyConnection));
159
160     }
161
162     @Test
163     public void Validate_Set_Get_Methods_Return_Correct_Values(){
164         feed.setName("testName");
165         feed.setVersion("v1.0");
166         feed.setGroupid(1);
167         feed.setDescription("test feed");
168         feed.setBusinessDescription("test feed");
169         feed.setSuspended(false);
170         feed.setPublisher("publish");
171
172         Assert.assertEquals(feed.getName(), "testName");
173         Assert.assertEquals(feed.getVersion(), "v1.0");
174         Assert.assertEquals(feed.getGroupid(), 1);
175         Assert.assertEquals(feed.getDescription(), "test feed");
176         Assert.assertEquals(feed.getBusinessDescription(), "test feed");
177         Assert.assertFalse(feed.isSuspended());
178         Assert.assertEquals(feed.getPublisher(), "publish");
179     }
180
181     @Test
182     public void Given_IsFeedValid_Called_And_Feed_Exists_Returns_True(){
183         Assert.assertTrue(Feed.isFeedValid(1));
184     }
185
186     private Connection CreateSpyForDbConnection() throws SQLException {
187         Connection conn = provDbUtils.getConnection();
188         return Mockito.spy(conn);
189     }
190 }