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