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