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.onap.dmaap.datarouter.node.config.NodeConfig;
35 import org.onap.dmaap.datarouter.node.config.ProvData;
36 import org.powermock.core.classloader.annotations.PowerMockIgnore;
37 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
38 import org.powermock.modules.junit4.PowerMockRunner;
40 @RunWith(PowerMockRunner.class)
41 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.config.ProvData"})
42 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
43 public class NodeConfigTest {
45 private static NodeConfig nodeConfig;
48 public static void setUp() throws IOException {
49 ProvData provData = setUpProvData();
50 nodeConfig = new NodeConfig(provData, "Name", "spool/dir", 80, "Key");
53 private static ProvData setUpProvData() throws IOException {
54 JSONObject provData = new JSONObject();
55 createValidFeed(provData);
56 createValidSubscription(provData);
57 createValidParameters(provData);
58 createValidIngressValues(provData);
59 createValidEgressValues(provData);
60 createValidRoutingValues(provData);
61 Reader reader = new StringReader(provData.toString());
62 return new ProvData(reader);
65 private static void createValidFeed(JSONObject provData) {
66 JSONArray feeds = new JSONArray();
67 JSONObject feed = new JSONObject();
68 JSONObject auth = new JSONObject();
69 JSONArray endpointIds = new JSONArray();
70 JSONArray endpointAddrs = new JSONArray();
71 JSONObject endpointId = new JSONObject();
72 feed.put("feedid", "1");
73 feed.put("name", "Feed1");
74 feed.put("version", "m1.0");
75 feed.put("suspend", false);
76 feed.put("deleted", false);
77 endpointId.put("id", "user1");
78 endpointId.put("password", "password1");
79 endpointIds.put(endpointId);
80 auth.put("endpoint_ids", endpointIds);
81 endpointAddrs.put("172.0.0.1");
82 auth.put("endpoint_addrs", endpointAddrs);
83 feed.put("authorization", auth);
85 provData.put("feeds", feeds);
88 private static void createValidSubscription(JSONObject provData) {
89 JSONArray subscriptions = new JSONArray();
90 JSONObject subscription = new JSONObject();
91 JSONObject delivery = new JSONObject();
92 subscription.put("subid", "1");
93 subscription.put("feedid", "1");
94 subscription.put("suspend", false);
95 subscription.put("metadataOnly", false);
96 delivery.put("url", "https://172.0.0.2");
97 delivery.put("user", "user1");
98 delivery.put("password", "password1");
99 delivery.put("use100", true);
100 subscription.put("delivery", delivery);
101 subscription.put("privilegedSubscriber", false);
102 subscription.put("follow_redirect", false);
103 subscription.put("decompress", false);
104 subscriptions.put(subscription);
105 provData.put("subscriptions", subscriptions);
108 private static void createValidParameters(JSONObject provData) {
109 JSONObject parameters = new JSONObject();
110 JSONArray nodes = new JSONArray();
111 parameters.put("PROV_NAME", "prov.datarouternew.com");
112 parameters.put("DELIVERY_INIT_RETRY_INTERVAL", "10");
113 parameters.put("DELIVERY_MAX_AGE", "86400");
114 parameters.put("PROV_DOMAIN", "");
115 nodes.put("172.0.0.4");
116 parameters.put("NODES", nodes);
117 provData.put("parameters", parameters);
120 private static void createValidIngressValues(JSONObject provData) {
121 JSONArray ingresses = new JSONArray();
122 JSONObject ingress = new JSONObject();
123 ingress.put("feedid", "1");
124 ingress.put("subnet", "");
125 ingress.put("user", "");
126 ingress.put("node", "172.0.0.4");
127 ingresses.put(ingress);
128 provData.put("ingress", ingresses);
131 private static void createValidEgressValues(JSONObject provData) {
132 JSONObject egress = new JSONObject();
133 egress.put("subid", "1");
134 egress.put("nodeid", "172.0.0.4");
135 provData.put("egress", egress);
138 private static void createValidRoutingValues(JSONObject provData) {
139 JSONArray routings = new JSONArray();
140 JSONObject routing = new JSONObject();
141 routing.put("from", "prov.datarouternew.com");
142 routing.put("to", "172.0.0.4");
143 routing.put("via", "172.100.0.1");
144 routings.put(routing);
145 provData.put("routing", routings);
149 public void Given_Feed_Does_Not_Exist_Then_Is_Publish_Permitted_Returns_Not_Null() {
150 String permitted = nodeConfig.isPublishPermitted("2", "user", "0.0.0.0");
151 Assert.assertEquals("Feed does not exist", permitted);
155 public void Given_Feed_But_User_Not_Permitted_Then_Is_Publish_Permitted_Returns_Not_Null() {
156 String permitted = nodeConfig.isPublishPermitted("1", "user", "0.0.0.0");
157 Assert.assertEquals("Publisher not permitted for this feed", permitted);
161 public void Given_Feed_But_Ip_Does_Not_Match_Then_Is_Publish_Permitted_Returns_Not_Null() {
162 String permitted = nodeConfig.isPublishPermitted("1", "Basic dXNlcjE6cGFzc3dvcmQx", "0.0.0.0");
163 Assert.assertEquals("Publisher not permitted for this feed", permitted);
167 public void Given_Feed_Then_Is_Publish_Permitted_Returns_Null() {
168 String permitted = nodeConfig.isPublishPermitted("1", "Basic dXNlcjE6cGFzc3dvcmQx", "172.0.0.1");
169 Assert.assertNull(permitted);
173 public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id() {
174 String feedId = nodeConfig.getFeedId("1");
175 Assert.assertEquals("1", feedId);
179 public void Given_Incorrect_SubId_Then_Get_Feed_Id_Returns_Null() {
180 String feedId = nodeConfig.getFeedId("2");
181 Assert.assertNull(feedId);
185 public void Given_SubId_Then_Get_Spool_Dir_Returns_Correct_Id() {
186 String spoolDir = nodeConfig.getSpoolDir("1");
187 Assert.assertEquals("spool/dir/s/0/1", spoolDir);
191 public void Given_Incorrect_SubId_Then_Get_Spool_Dir_Returns_Null() {
192 String spoolDir = nodeConfig.getSpoolDir("2");
193 Assert.assertNull(spoolDir);
197 public void Given_Feed_And_Incorrect_Credentials_Then_Get_Auth_User_Returns_Null() {
198 String authUser = nodeConfig.getAuthUser("1", "incorrect");
199 Assert.assertNull(authUser);
203 public void Given_Feed_And_Correct_Credentials_Then_Get_Auth_User_Returns_User() {
204 String authUser = nodeConfig.getAuthUser("1", "Basic dXNlcjE6cGFzc3dvcmQx");
205 Assert.assertEquals("user1", authUser);
209 public void Given_Correct_Feed_Then_Get_Ingress_Node_Returns_Node() {
210 String node = nodeConfig.getIngressNode("1", "user1", "172.0.0.1");
211 Assert.assertEquals("172.0.0.4", node);
215 public void Given_Correct_Feed_Then_Get_Targets_Returns_Correct_Dest_Info() {
216 Target[] targets = nodeConfig.getTargets("1");
217 Assert.assertEquals("1", targets[0].getDestInfo().getSubId());
218 Assert.assertEquals("spool/dir/s/0/1", targets[0].getDestInfo().getSpool());
221 @Test(expected = ArrayIndexOutOfBoundsException.class)
222 public void Given_Null_Feed_Then_Get_Targets_Returns_Empty_Array() {
223 Target[] targets = nodeConfig.getTargets(null);
224 targets[0].getDestInfo();
227 @Test(expected = ArrayIndexOutOfBoundsException.class)
228 public void Given_Incorrect_Feed_Then_Get_Targets_Returns_Empty_Array() {
229 Target[] targets = nodeConfig.getTargets("2");
230 targets[0].getDestInfo();
234 public void Given_Same_Ip_Then_Is_Another_Node_Returns_False() {
235 Boolean isAnotherNode =
236 nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OmtCTmhkWVFvbzhXNUphZ2g4T1N4Zmp6Mzl1ND0=", "172.0.0.1");
237 Assert.assertFalse(isAnotherNode);
241 public void Given_Different_Ip_Then_Is_Another_Node_Returns_True() {
242 Boolean isAnotherNode =
243 nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OjlKOEFMUEhWQ2FpZ3FnZFpMMlRMYVRKSE1QQS8wNjdjR2JhV2RaUU1XSG1MNk5KbEtBVmpPbWtoZTR6ZmVlYjJzbElNMVR0REc2b0tYb0dLSDRMa1BBPT0=", "172.0.0.4");
244 Assert.assertTrue(isAnotherNode);
248 public void Given_Param_Name_Then_Get_Prov_Param_Returns_Parameter() {
249 String paramValue = nodeConfig.getProvParam("DELIVERY_MAX_AGE");
250 Assert.assertEquals("86400", paramValue);
254 public void Validate_Get_All_Dests_Returns_Dest_Info() {
255 DestInfo[] destInfo = nodeConfig.getAllDests();
256 Assert.assertEquals("n:172.0.0.4", destInfo[0].getName());
260 public void Validate_Get_MyAuth_Returns_Correct_Auth() {
261 String auth = nodeConfig.getMyAuth();
262 Assert.assertEquals("Basic TmFtZTo3YTRsQkxqMENQQ3lEbVVPaUI5Tks3b0pSeGROVUxqZzNNUHpkcEFYNHcvN09DNVR5S1hhWFA0MGR5aHhzbm90bXM4d1BGeXdHVlQ3MTJhcldXSFR1dz09", auth);