1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
23 package org.onap.dmaap.datarouter.node;
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;
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 {
43 private static NodeConfig nodeConfig;
46 public static void setUp() throws IOException {
47 ProvData provData = setUpProvData();
48 nodeConfig = new NodeConfig(provData, "Name", "spool/dir", 80, "Key");
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);
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");
84 provData.put("feeds", feeds);
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);
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);
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);
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);
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);
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);
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);
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);
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);
172 public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id() {
173 String feedId = nodeConfig.getFeedId("1");
174 Assert.assertEquals("1", feedId);
178 public void Given_Incorrect_SubId_Then_Get_Feed_Id_Returns_Null() {
179 String feedId = nodeConfig.getFeedId("2");
180 Assert.assertNull(feedId);
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);
190 public void Given_Incorrect_SubId_Then_Get_Spool_Dir_Returns_Null() {
191 String spoolDir = nodeConfig.getSpoolDir("2");
192 Assert.assertNull(spoolDir);
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);
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);
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);
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());
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();
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();
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);
240 public void Given_Different_Ip_Then_Is_Another_Node_Returns_True() {
241 Boolean isAnotherNode =
242 nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OjlKOEFMUEhWQ2FpZ3FnZFpMMlRMYVRKSE1QQS8wNjdjR2JhV2RaUU1XSG1MNk5KbEtBVmpPbWtoZTR6ZmVlYjJzbElNMVR0REc2b0tYb0dLSDRMa1BBPT0=", "172.0.0.4");
243 Assert.assertTrue(isAnotherNode);
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);
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());
259 public void Validate_Get_MyAuth_Returns_Correct_Auth() {
260 String auth = nodeConfig.getMyAuth();
261 Assert.assertEquals("Basic TmFtZTo3YTRsQkxqMENQQ3lEbVVPaUI5Tks3b0pSeGROVUxqZzNNUHpkcEFYNHcvN09DNVR5S1hhWFA0MGR5aHhzbm90bXM4d1BGeXdHVlQ3MTJhcldXSFR1dz09", auth);