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