Merge "Add Tests for StatisticsServlet and Group Class"
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / NodeConfigTest.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.node;
24
25 import org.json.JSONArray;
26 import org.json.JSONObject;
27 import org.junit.Assert;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.powermock.modules.junit4.PowerMockRunner;
32
33 import java.io.IOException;
34 import java.io.Reader;
35 import java.io.StringReader;
36
37 @RunWith(PowerMockRunner.class)
38 public class NodeConfigTest {
39
40     private static NodeConfig nodeConfig;
41
42     @BeforeClass
43     public static void setUp() throws IOException{
44         ProvData provData = setUpProvData();
45         nodeConfig = new NodeConfig(provData, "Name", "spool/dir", 80, "Key");
46     }
47
48     @Test
49     public void Given_Feed_Does_Not_Exist_Then_Is_Publish_Permitted_Returns_Not_Null() {
50         String permitted = nodeConfig.isPublishPermitted("2", "user", "0.0.0.0");
51         Assert.assertEquals("Feed does not exist", permitted);
52     }
53
54     @Test
55     public void Given_Feed_But_User_Not_Permitted_Then_Is_Publish_Permitted_Returns_Not_Null() {
56         String permitted = nodeConfig.isPublishPermitted("1", "user", "0.0.0.0");
57         Assert.assertEquals("Publisher not permitted for this feed", permitted);
58     }
59
60     @Test
61     public void Given_Feed_But_Ip_Does_Not_Match_Then_Is_Publish_Permitted_Returns_Not_Null() {
62         String permitted = nodeConfig.isPublishPermitted("1", "Basic dXNlcjE6cGFzc3dvcmQx", "0.0.0.0");
63         Assert.assertEquals("Publisher not permitted for this feed", permitted);
64     }
65
66     @Test
67     public void Given_Feed_Then_Is_Publish_Permitted_Returns_Null() {
68         String permitted = nodeConfig.isPublishPermitted("1", "Basic dXNlcjE6cGFzc3dvcmQx", "172.0.0.1");
69         Assert.assertNull(permitted);
70     }
71
72     @Test
73     public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id() {
74         String feedId = nodeConfig.getFeedId("1");
75         Assert.assertEquals("1", feedId);
76     }
77
78     @Test
79     public void Given_Incorrect_SubId_Then_Get_Feed_Id_Returns_Null() {
80         String feedId = nodeConfig.getFeedId("2");
81         Assert.assertNull(feedId);
82     }
83
84     @Test
85     public void Given_SubId_Then_Get_Spool_Dir_Returns_Correct_Id() {
86         String spoolDir = nodeConfig.getSpoolDir("1");
87         Assert.assertEquals("spool/dir/s/0/1", spoolDir);
88     }
89
90     @Test
91     public void Given_Incorrect_SubId_Then_Get_Spool_Dir_Returns_Null() {
92         String spoolDir = nodeConfig.getSpoolDir("2");
93         Assert.assertNull(spoolDir);
94     }
95
96     @Test
97     public void Given_Feed_And_Incorrect_Credentials_Then_Get_Auth_User_Returns_Null() {
98         String authUser = nodeConfig.getAuthUser("1", "incorrect");
99         Assert.assertNull(authUser);
100     }
101
102     @Test
103     public void Given_Feed_And_Correct_Credentials_Then_Get_Auth_User_Returns_User() {
104         String authUser = nodeConfig.getAuthUser("1", "Basic dXNlcjE6cGFzc3dvcmQx");
105         Assert.assertEquals("user1", authUser);
106     }
107
108     @Test
109     public void Given_Correct_Feed_Then_Get_Ingress_Node_Returns_Node() {
110         String node = nodeConfig.getIngressNode("1", "user1", "172.0.0.1");
111         Assert.assertEquals("172.0.0.4", node);
112     }
113
114     @Test
115     public void Given_Correct_Feed_Then_Get_Targets_Returns_Correct_Dest_Info() {
116         Target[] targets = nodeConfig.getTargets("1");
117         Assert.assertEquals("1", targets[0].getDestInfo().getSubId());
118         Assert.assertEquals("spool/dir/s/0/1", targets[0].getDestInfo().getSpool());
119     }
120
121     @Test(expected = ArrayIndexOutOfBoundsException.class)
122     public void Given_Null_Feed_Then_Get_Targets_Returns_Empty_Array() {
123         Target[] targets = nodeConfig.getTargets(null);
124         targets[0].getDestInfo();
125     }
126
127     @Test(expected = ArrayIndexOutOfBoundsException.class)
128     public void Given_Incorrect_Feed_Then_Get_Targets_Returns_Empty_Array() {
129         Target[] targets = nodeConfig.getTargets("2");
130         targets[0].getDestInfo();
131     }
132
133     @Test
134     public void Given_Same_Ip_Then_Is_Another_Node_Returns_False() {
135         Boolean isAnotherNode = nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OmtCTmhkWVFvbzhXNUphZ2g4T1N4Zmp6Mzl1ND0=", "172.0.0.1");
136         Assert.assertFalse(isAnotherNode);
137     }
138
139     @Test
140     public void Given_Different_Ip_Then_Is_Another_Node_Returns_True() {
141         Boolean isAnotherNode = nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OmtCTmhkWVFvbzhXNUphZ2g4T1N4Zmp6Mzl1ND0=", "172.0.0.4");
142         Assert.assertTrue(isAnotherNode);
143     }
144
145     @Test
146     public void Given_Param_Name_Then_Get_Prov_Param_Returns_Parameter() {
147         String paramValue = nodeConfig.getProvParam("DELIVERY_MAX_AGE");
148         Assert.assertEquals("86400", paramValue);
149     }
150
151     @Test
152     public void Validate_Get_All_Dests_Returns_Dest_Info() {
153         DestInfo[] destInfo = nodeConfig.getAllDests();
154         Assert.assertEquals("n:172.0.0.4", destInfo[0].getName());
155     }
156
157     @Test
158     public void Validate_Get_MyAuth_Returns_Correct_Auth() {
159         String auth = nodeConfig.getMyAuth();
160         Assert.assertEquals("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9", auth);
161     }
162
163     private static ProvData setUpProvData() throws IOException {
164         JSONObject provData = new JSONObject();
165         createValidFeed(provData);
166         createValidSubscription(provData);
167         createValidParameters(provData);
168         createValidIngressValues(provData);
169         createValidEgressValues(provData);
170         createValidRoutingValues(provData);
171         Reader reader = new StringReader(provData.toString());
172         return new ProvData(reader);
173     }
174
175     private static void createValidFeed(JSONObject provData) {
176         JSONArray feeds = new JSONArray();
177         JSONObject feed = new JSONObject();
178         JSONObject auth = new JSONObject();
179         JSONArray endpointIds = new JSONArray();
180         JSONArray endpointAddrs = new JSONArray();
181         JSONObject endpointId = new JSONObject();
182         feed.put("feedid", "1");
183         feed.put("name", "Feed1");
184         feed.put("version", "m1.0");
185         feed.put("suspend", false);
186         feed.put("deleted", false);
187         endpointId.put("id", "user1");
188         endpointId.put("password", "password1");
189         endpointIds.put(endpointId);
190         auth.put("endpoint_ids", endpointIds);
191         endpointAddrs.put("172.0.0.1");
192         auth.put("endpoint_addrs", endpointAddrs);
193         feed.put("authorization", auth);
194         feeds.put(feed);
195         provData.put("feeds", feeds);
196     }
197
198     private static void createValidSubscription(JSONObject provData) {
199         JSONArray subscriptions = new JSONArray();
200         JSONObject subscription = new JSONObject();
201         JSONObject delivery = new JSONObject();
202         subscription.put("subid", "1");
203         subscription.put("feedid", "1");
204         subscription.put("suspend", false);
205         subscription.put("metadataOnly", false);
206         delivery.put("url", "https://172.0.0.2");
207         delivery.put("user", "user1");
208         delivery.put("password", "password1");
209         delivery.put("use100", true);
210         subscription.put("delivery", delivery);
211         subscriptions.put(subscription);
212         provData.put("subscriptions", subscriptions);
213     }
214
215     private static void createValidParameters(JSONObject provData) {
216         JSONObject parameters = new JSONObject();
217         JSONArray nodes = new JSONArray();
218         parameters.put("PROV_NAME", "prov.datarouternew.com");
219         parameters.put("DELIVERY_INIT_RETRY_INTERVAL", "10");
220         parameters.put("DELIVERY_MAX_AGE", "86400");
221         parameters.put("PROV_DOMAIN", "");
222         nodes.put("172.0.0.4");
223         parameters.put("NODES", nodes);
224         provData.put("parameters", parameters);
225     }
226
227     private static void createValidIngressValues(JSONObject provData) {
228         JSONArray ingresses = new JSONArray();
229         JSONObject ingress = new JSONObject();
230         ingress.put("feedid", "1");
231         ingress.put("subnet", "");
232         ingress.put("user", "");
233         ingress.put("node", "172.0.0.4");
234         ingresses.put(ingress);
235         provData.put("ingress", ingresses);
236     }
237
238     private static void createValidEgressValues(JSONObject provData) {
239         JSONObject egress = new JSONObject();
240         egress.put("subid", "1");
241         egress.put("nodeid", "172.0.0.4");
242         provData.put("egress", egress);
243     }
244
245     private static void createValidRoutingValues(JSONObject provData) {
246         JSONArray routings = new JSONArray();
247         JSONObject routing = new JSONObject();
248         routing.put("from", "prov.datarouternew.com");
249         routing.put("to", "172.0.0.4");
250         routing.put("via", "172.100.0.1");
251         routings.put(routing);
252         provData.put("routing", routings);
253     }
254 }