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