1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*******************************************************************************
* ============LICENSE_START==================================================
* * org.onap.dmaap
* * ===========================================================================
* * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
* * ===========================================================================
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* * ============LICENSE_END====================================================
* *
* * ECOMP is a trademark and service mark of AT&T Intellectual Property.
* *
******************************************************************************/
package org.onap.dmaap.datarouter.provisioning.beans;
import org.json.JSONObject;
import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.onap.dmaap.datarouter.provisioning.utils.DB;
import org.powermock.modules.junit4.PowerMockRunner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.io.InvalidObjectException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.mockito.Matchers.anyString;
@RunWith(PowerMockRunner.class)
public class FeedTest {
private static EntityManagerFactory emf;
private static EntityManager em;
private Feed feed;
private DB db;
@BeforeClass
public static void init() {
emf = Persistence.createEntityManagerFactory("dr-unit-tests");
em = emf.createEntityManager();
System.setProperty(
"org.onap.dmaap.datarouter.provserver.properties",
"src/test/resources/h2Database.properties");
}
@AfterClass
public static void tearDownClass() {
em.clear();
em.close();
emf.close();
}
@Before
public void setUp() throws Exception {
db = new DB();
feed = new Feed("Feed1","v0.1", "First Feed for testing", "First Feed for testing");
feed.setFeedid(1);
feed.setGroupid(1);
feed.setPublisher("pub");
feed.setDeleted(false);
}
@Test
public void Given_getFilteredFeedUrlList_With_Name_Then_Method_Returns_Self_Links() {
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");
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");
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());
Assert.assertEquals(true, bool);
JSONObject jo = new JSONObject();
jo.put("self","self_link");
jo.put("publish","publish_link");
jo.put("subscribe","subscribe_link");
jo.put("log","log_link");
feed.setLinks(new FeedLinks(jo));
bool = feed.doInsert(db.getConnection());
Assert.assertEquals(true, bool);
}
@Test
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() {
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 = db.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.setBusiness_description("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.getBusiness_description(), "test feed");
Assert.assertEquals(feed.isSuspended(), false);
Assert.assertEquals(feed.getPublisher(), "publish");
}
@Test
public void Given_IsFeedValid_Called_And_Feed_Exists_Returns_True(){
Assert.assertEquals(feed.isFeedValid(1), true);
}
private Connection CreateSpyForDbConnection() throws SQLException {
Connection conn = db.getConnection();
return Mockito.spy(conn);
}
}
|