Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / FeedTest.java
index 907aa93..ec40107 100644 (file)
  ******************************************************************************/
 package org.onap.dmaap.datarouter.provisioning.beans;
 
-import org.json.JSONObject;
-import org.junit.*;
-import org.junit.runner.RunWith;
-import org.onap.dmaap.datarouter.provisioning.utils.DB;
-import org.powermock.modules.junit4.PowerMockRunner;
+import static org.mockito.Matchers.anyString;
 
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
-import java.io.InvalidObjectException;
-import java.sql.SQLException;
-import java.util.List;
+import org.json.JSONObject;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mockito;
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
+import org.powermock.modules.junit4.PowerMockRunner;
 
 @RunWith(PowerMockRunner.class)
 public class FeedTest {
     private static EntityManagerFactory emf;
     private static EntityManager em;
     private Feed feed;
-    private DB db;
+    private ProvDbUtils provDbUtils;
 
     @BeforeClass
     public static void init() {
@@ -60,7 +68,7 @@ public class FeedTest {
 
     @Before
     public void setUp() throws Exception {
-        db = new DB();
+        provDbUtils = ProvDbUtils.getInstance();
         feed = new Feed("Feed1","v0.1", "First Feed for testing", "First Feed for testing");
         feed.setFeedid(1);
         feed.setGroupid(1);
@@ -70,25 +78,25 @@ public class FeedTest {
 
     @Test
     public void Given_getFilteredFeedUrlList_With_Name_Then_Method_Returns_Self_Links() {
-        List<String>  list= feed.getFilteredFeedUrlList("name","Feed1");
+        List<String>  list= Feed.getFilteredFeedUrlList("name","Feed1");
         Assert.assertEquals("self_link",list.get(0));
     }
 
     @Test
     public void Given_getFilteredFeedUrlList_With_Publ_Then_Method_Returns_Self_Links() {
-        List<String>  list= feed.getFilteredFeedUrlList("publ","pub");
+        List<String>  list= Feed.getFilteredFeedUrlList("publ","pub");
         Assert.assertEquals("self_link",list.get(0));
     }
 
     @Test
     public void Given_getFilteredFeedUrlList_With_Subs_Then_Method_Returns_Self_Links() {
-        List<String>  list= feed.getFilteredFeedUrlList("subs","sub123");
+        List<String>  list= Feed.getFilteredFeedUrlList("subs","sub123");
         Assert.assertEquals("self_link",list.get(0));
     }
 
     @Test
-    public void Given_doDelete_Succeeds_Then_doInsert_To_Put_Feed_Back_And_Bool_Is_True() throws SQLException, InvalidObjectException {
-        Boolean bool = feed.doDelete(db.getConnection());
+    public void Given_doDelete_Succeeds_Then_doInsert_To_Put_Feed_Back_And_Bool_Is_True() throws SQLException {
+        Boolean bool = feed.doDelete(provDbUtils.getConnection());
         Assert.assertEquals(true, bool);
         JSONObject jo = new JSONObject();
         jo.put("self","self_link");
@@ -96,22 +104,85 @@ public class FeedTest {
         jo.put("subscribe","subscribe_link");
         jo.put("log","log_link");
         feed.setLinks(new FeedLinks(jo));
-        bool = feed.doInsert(db.getConnection());
+        bool = feed.doInsert(provDbUtils.getConnection());
         Assert.assertEquals(true, bool);
     }
 
     @Test
-    public void Validate_ChaneOwnerShip_Returns_True()
-    {
+    public void Validate_ChaneOwnerShip_Returns_True() {
         Boolean bool = feed.changeOwnerShip();
         Assert.assertEquals(true, bool);
     }
 
     @Test
-    public void Given_Feeds_Are_Equal_Then_Equals_Returns_True()
-    {
+    public void Given_Feeds_Are_Equal_Then_Equals_Returns_True() {
         Feed feed2 = feed;
         Boolean bool = feed.equals(feed2);
         Assert.assertEquals(true, bool);
     }
+
+    @Test
+    public void Validate_getFeedByNameVersion_Returns_Valid_Feed() {
+        feed = Feed.getFeedByNameVersion("Feed1","v0.1");
+        Assert.assertEquals(feed.toString(), "FEED: feedid=1, name=Feed1, version=v0.1");
+    }
+
+    @Test
+    public void Given_doDelete_Throws_SQLException_Then_Returns_False() throws SQLException {
+        Connection spyConnection = CreateSpyForDbConnection();
+        Mockito.doThrow(new SQLException()).when(spyConnection).prepareStatement(anyString());
+        Assert.assertEquals(feed.doDelete(spyConnection), false);
+    }
+
+    @Test
+    public void Given_doInsert_Throws_SQLException_Then_Returns_False() throws SQLException {
+        Connection connection = provDbUtils.getConnection();
+        FeedAuthorization fa = new FeedAuthorization();
+        Set setA = new HashSet();
+        setA.add(new FeedEndpointID("1", "Name"));
+        Set setB = new HashSet();
+        setB.add("172.0.0.1");
+        fa.setEndpointIDS(setA);
+        fa.setEndpointAddrs(setB);
+        feed.setAuthorization(fa);
+        Assert.assertEquals(feed.doInsert(connection), false);
+
+    }
+
+    @Test
+    public void Given_doUpdate_Throws_SQLException_Then_Returns_False() throws SQLException {
+        Connection spyConnection = CreateSpyForDbConnection();
+        Mockito.doThrow(new SQLException()).when(spyConnection).prepareStatement(anyString());
+        Assert.assertEquals(feed.doUpdate(spyConnection), false);
+
+    }
+
+    @Test
+    public void Validate_Set_Get_Methods_Return_Correct_Values(){
+        feed.setName("testName");
+        feed.setVersion("v1.0");
+        feed.setGroupid(1);
+        feed.setDescription("test feed");
+        feed.setBusinessDescription("test feed");
+        feed.setSuspended(false);
+        feed.setPublisher("publish");
+
+        Assert.assertEquals(feed.getName(), "testName");
+        Assert.assertEquals(feed.getVersion(), "v1.0");
+        Assert.assertEquals(feed.getGroupid(), 1);
+        Assert.assertEquals(feed.getDescription(), "test feed");
+        Assert.assertEquals(feed.getBusinessDescription(), "test feed");
+        Assert.assertEquals(feed.isSuspended(), false);
+        Assert.assertEquals(feed.getPublisher(), "publish");
+    }
+
+    @Test
+    public void Given_IsFeedValid_Called_And_Feed_Exists_Returns_True(){
+        Assert.assertTrue(Feed.isFeedValid(1));
+    }
+
+    private Connection CreateSpyForDbConnection() throws SQLException {
+        Connection conn = provDbUtils.getConnection();
+        return Mockito.spy(conn);
+    }
 }
\ No newline at end of file