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