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 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;
34 import java.io.IOException;
35 import java.io.Reader;
36 import java.io.StringReader;
38 @RunWith(PowerMockRunner.class)
39 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.ProvData", "org.onap.dmaap.datarouter.node.NodeUtils"})
40 public class NodeConfigTest {
42 private static NodeConfig nodeConfig;
45 public static void setUp() throws IOException{
46 ProvData provData = setUpProvData();
47 nodeConfig = new NodeConfig(provData, "Name", "spool/dir", 80, "Key");
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);
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);
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);
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);
75 public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id() {
76 String feedId = nodeConfig.getFeedId("1");
77 Assert.assertEquals("1", feedId);
81 public void Given_Incorrect_SubId_Then_Get_Feed_Id_Returns_Null() {
82 String feedId = nodeConfig.getFeedId("2");
83 Assert.assertNull(feedId);
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);
93 public void Given_Incorrect_SubId_Then_Get_Spool_Dir_Returns_Null() {
94 String spoolDir = nodeConfig.getSpoolDir("2");
95 Assert.assertNull(spoolDir);
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);
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);
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);
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());
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();
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();
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);
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);
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);
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());
160 public void Validate_Get_MyAuth_Returns_Correct_Auth() {
161 String auth = nodeConfig.getMyAuth();
162 Assert.assertEquals("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9", auth);
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);
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);
197 provData.put("feeds", feeds);
200 private static void createValidSubscription(JSONObject provData) {
201 JSONArray subscriptions = new JSONArray();
202 JSONObject subscription = new JSONObject();
203 JSONObject delivery = new JSONObject();
204 subscription.put("subid", "1");
205 subscription.put("feedid", "1");
206 subscription.put("suspend", false);
207 subscription.put("metadataOnly", false);
208 delivery.put("url", "https://172.0.0.2");
209 delivery.put("user", "user1");
210 delivery.put("password", "password1");
211 delivery.put("use100", true);
212 subscription.put("delivery", delivery);
213 subscriptions.put(subscription);
214 provData.put("subscriptions", subscriptions);
217 private static void createValidParameters(JSONObject provData) {
218 JSONObject parameters = new JSONObject();
219 JSONArray nodes = new JSONArray();
220 parameters.put("PROV_NAME", "prov.datarouternew.com");
221 parameters.put("DELIVERY_INIT_RETRY_INTERVAL", "10");
222 parameters.put("DELIVERY_MAX_AGE", "86400");
223 parameters.put("PROV_DOMAIN", "");
224 nodes.put("172.0.0.4");
225 parameters.put("NODES", nodes);
226 provData.put("parameters", parameters);
229 private static void createValidIngressValues(JSONObject provData) {
230 JSONArray ingresses = new JSONArray();
231 JSONObject ingress = new JSONObject();
232 ingress.put("feedid", "1");
233 ingress.put("subnet", "");
234 ingress.put("user", "");
235 ingress.put("node", "172.0.0.4");
236 ingresses.put(ingress);
237 provData.put("ingress", ingresses);
240 private static void createValidEgressValues(JSONObject provData) {
241 JSONObject egress = new JSONObject();
242 egress.put("subid", "1");
243 egress.put("nodeid", "172.0.0.4");
244 provData.put("egress", egress);
247 private static void createValidRoutingValues(JSONObject provData) {
248 JSONArray routings = new JSONArray();
249 JSONObject routing = new JSONObject();
250 routing.put("from", "prov.datarouternew.com");
251 routing.put("to", "172.0.0.4");
252 routing.put("via", "172.100.0.1");
253 routings.put(routing);
254 provData.put("routing", routings);