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