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