1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.provisioning.beans;
25 import org.json.JSONObject;
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;
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;
42 import static org.mockito.Matchers.anyString;
44 @RunWith(PowerMockRunner.class)
45 public class FeedTest {
46 private static EntityManagerFactory emf;
47 private static EntityManager em;
52 public static void init() {
53 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
54 em = emf.createEntityManager();
56 "org.onap.dmaap.datarouter.provserver.properties",
57 "src/test/resources/h2Database.properties");
61 public static void tearDownClass() {
68 public void setUp() throws Exception {
70 feed = new Feed("Feed1","v0.1", "First Feed for testing", "First Feed for testing");
73 feed.setPublisher("pub");
74 feed.setDeleted(false);
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));
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));
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));
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);
110 public void Validate_ChaneOwnerShip_Returns_True() {
111 Boolean bool = feed.changeOwnerShip();
112 Assert.assertEquals(true, bool);
116 public void Given_Feeds_Are_Equal_Then_Equals_Returns_True() {
118 Boolean bool = feed.equals(feed2);
119 Assert.assertEquals(true, bool);
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");
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);
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);
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);
159 public void Validate_Set_Get_Methods_Return_Correct_Values(){
160 feed.setName("testName");
161 feed.setVersion("v1.0");
163 feed.setDescription("test feed");
164 feed.setBusiness_description("test feed");
165 feed.setSuspended(false);
166 feed.setPublisher("publish");
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");
178 public void Given_IsFeedValid_Called_And_Feed_Exists_Returns_True(){
179 Assert.assertEquals(feed.isFeedValid(1), true);
182 private Connection CreateSpyForDbConnection() throws SQLException {
183 Connection conn = db.getConnection();
184 return Mockito.spy(conn);