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 static org.mockito.Matchers.anyString;
27 import java.sql.Connection;
28 import java.sql.SQLException;
29 import java.util.HashSet;
30 import java.util.List;
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;
46 @RunWith(PowerMockRunner.class)
47 public class FeedTest {
48 private static EntityManagerFactory emf;
49 private static EntityManager em;
51 private ProvDbUtils provDbUtils;
54 public static void init() {
55 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
56 em = emf.createEntityManager();
58 "org.onap.dmaap.datarouter.provserver.properties",
59 "src/test/resources/h2Database.properties");
63 public static void tearDownClass() {
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");
75 feed.setPublisher("pub");
76 feed.setDeleted(false);
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));
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));
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));
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);
112 public void Validate_ChaneOwnerShip_Returns_True() {
113 Boolean bool = feed.changeOwnerShip();
114 Assert.assertEquals(true, bool);
118 public void Given_Feeds_Are_Equal_Then_Equals_Returns_True() {
120 Boolean bool = feed.equals(feed2);
121 Assert.assertEquals(true, bool);
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");
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);
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);
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);
161 public void Validate_Set_Get_Methods_Return_Correct_Values(){
162 feed.setName("testName");
163 feed.setVersion("v1.0");
165 feed.setDescription("test feed");
166 feed.setBusinessDescription("test feed");
167 feed.setSuspended(false);
168 feed.setPublisher("publish");
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");
180 public void Given_IsFeedValid_Called_And_Feed_Exists_Returns_True(){
181 Assert.assertTrue(Feed.isFeedValid(1));
184 private Connection CreateSpyForDbConnection() throws SQLException {
185 Connection conn = provDbUtils.getConnection();
186 return Mockito.spy(conn);