aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/NodeConfigTest.java
blob: 1272f3c2144aa94a0051f8fb998daff93e4dd1fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * ============LICENSE_START==================================================
 * * org.onap.dmaap
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * *
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.dmaap.datarouter.node;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.dmaap.datarouter.node.config.NodeConfig;
import org.onap.dmaap.datarouter.node.config.ProvData;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.config.ProvData"})
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
public class NodeConfigTest {

    private static NodeConfig nodeConfig;

    @BeforeClass
    public static void setUp() throws IOException {
        ProvData provData = setUpProvData();
        nodeConfig = new NodeConfig(provData, "Name", "spool/dir", 80, "Key");
    }

    private static ProvData setUpProvData() throws IOException {
        JSONObject provData = new JSONObject();
        createValidFeed(provData);
        createValidSubscription(provData);
        createValidParameters(provData);
        createValidIngressValues(provData);
        createValidEgressValues(provData);
        createValidRoutingValues(provData);
        Reader reader = new StringReader(provData.toString());
        return new ProvData(reader);
    }

    private static void createValidFeed(JSONObject provData) {
        JSONArray feeds = new JSONArray();
        JSONObject feed = new JSONObject();
        JSONObject auth = new JSONObject();
        JSONArray endpointIds = new JSONArray();
        JSONArray endpointAddrs = new JSONArray();
        JSONObject endpointId = new JSONObject();
        feed.put("feedid", "1");
        feed.put("name", "Feed1");
        feed.put("version", "m1.0");
        feed.put("suspend", false);
        feed.put("deleted", false);
        endpointId.put("id", "user1");
        endpointId.put("password", "password1");
        endpointIds.put(endpointId);
        auth.put("endpoint_ids", endpointIds);
        endpointAddrs.put("172.0.0.1");
        auth.put("endpoint_addrs", endpointAddrs);
        feed.put("authorization", auth);
        feeds.put(feed);
        provData.put("feeds", feeds);
    }

    private static void createValidSubscription(JSONObject provData) {
        JSONArray subscriptions = new JSONArray();
        JSONObject subscription = new JSONObject();
        JSONObject delivery = new JSONObject();
        subscription.put("subid", "1");
        subscription.put("feedid", "1");
        subscription.put("suspend", false);
        subscription.put("metadataOnly", false);
        delivery.put("url", "https://172.0.0.2");
        delivery.put("user", "user1");
        delivery.put("password", "password1");
        delivery.put("use100", true);
        subscription.put("delivery", delivery);
        subscription.put("privilegedSubscriber", false);
        subscription.put("follow_redirect", false);
        subscription.put("decompress", false);
        subscriptions.put(subscription);
        provData.put("subscriptions", subscriptions);
    }

    private static void createValidParameters(JSONObject provData) {
        JSONObject parameters = new JSONObject();
        JSONArray nodes = new JSONArray();
        parameters.put("PROV_NAME", "prov.datarouternew.com");
        parameters.put("DELIVERY_INIT_RETRY_INTERVAL", "10");
        parameters.put("DELIVERY_MAX_AGE", "86400");
        parameters.put("PROV_DOMAIN", "");
        nodes.put("172.0.0.4");
        parameters.put("NODES", nodes);
        provData.put("parameters", parameters);
    }

    private static void createValidIngressValues(JSONObject provData) {
        JSONArray ingresses = new JSONArray();
        JSONObject ingress = new JSONObject();
        ingress.put("feedid", "1");
        ingress.put("subnet", "");
        ingress.put("user", "");
        ingress.put("node", "172.0.0.4");
        ingresses.put(ingress);
        provData.put("ingress", ingresses);
    }

    private static void createValidEgressValues(JSONObject provData) {
        JSONObject egress = new JSONObject();
        egress.put("subid", "1");
        egress.put("nodeid", "172.0.0.4");
        provData.put("egress", egress);
    }

    private static void createValidRoutingValues(JSONObject provData) {
        JSONArray routings = new JSONArray();
        JSONObject routing = new JSONObject();
        routing.put("from", "prov.datarouternew.com");
        routing.put("to", "172.0.0.4");
        routing.put("via", "172.100.0.1");
        routings.put(routing);
        provData.put("routing", routings);
    }

    @Test
    public void Given_Feed_Does_Not_Exist_Then_Is_Publish_Permitted_Returns_Not_Null() {
        String permitted = nodeConfig.isPublishPermitted("2", "user", "0.0.0.0");
        Assert.assertEquals("Feed does not exist", permitted);
    }

    @Test
    public void Given_Feed_But_User_Not_Permitted_Then_Is_Publish_Permitted_Returns_Not_Null() {
        String permitted = nodeConfig.isPublishPermitted("1", "user", "0.0.0.0");
        Assert.assertEquals("Publisher not permitted for this feed", permitted);
    }

    @Test
    public void Given_Feed_But_Ip_Does_Not_Match_Then_Is_Publish_Permitted_Returns_Not_Null() {
        String permitted = nodeConfig.isPublishPermitted("1", "Basic dXNlcjE6cGFzc3dvcmQx", "0.0.0.0");
        Assert.assertEquals("Publisher not permitted for this feed", permitted);
    }

    @Test
    public void Given_Feed_Then_Is_Publish_Permitted_Returns_Null() {
        String permitted = nodeConfig.isPublishPermitted("1", "Basic dXNlcjE6cGFzc3dvcmQx", "172.0.0.1");
        Assert.assertNull(permitted);
    }

    @Test
    public void Given_SubId_Then_Get_Feed_Id_Returns_Correct_Id() {
        String feedId = nodeConfig.getFeedId("1");
        Assert.assertEquals("1", feedId);
    }

    @Test
    public void Given_Incorrect_SubId_Then_Get_Feed_Id_Returns_Null() {
        String feedId = nodeConfig.getFeedId("2");
        Assert.assertNull(feedId);
    }

    @Test
    public void Given_SubId_Then_Get_Spool_Dir_Returns_Correct_Id() {
        String spoolDir = nodeConfig.getSpoolDir("1");
        Assert.assertEquals("spool/dir/s/0/1", spoolDir);
    }

    @Test
    public void Given_Incorrect_SubId_Then_Get_Spool_Dir_Returns_Null() {
        String spoolDir = nodeConfig.getSpoolDir("2");
        Assert.assertNull(spoolDir);
    }

    @Test
    public void Given_Feed_And_Incorrect_Credentials_Then_Get_Auth_User_Returns_Null() {
        String authUser = nodeConfig.getAuthUser("1", "incorrect");
        Assert.assertNull(authUser);
    }

    @Test
    public void Given_Feed_And_Correct_Credentials_Then_Get_Auth_User_Returns_User() {
        String authUser = nodeConfig.getAuthUser("1", "Basic dXNlcjE6cGFzc3dvcmQx");
        Assert.assertEquals("user1", authUser);
    }

    @Test
    public void Given_Correct_Feed_Then_Get_Ingress_Node_Returns_Node() {
        String node = nodeConfig.getIngressNode("1", "user1", "172.0.0.1");
        Assert.assertEquals("172.0.0.4", node);
    }

    @Test
    public void Given_Correct_Feed_Then_Get_Targets_Returns_Correct_Dest_Info() {
        Target[] targets = nodeConfig.getTargets("1");
        Assert.assertEquals("1", targets[0].getDestInfo().getSubId());
        Assert.assertEquals("spool/dir/s/0/1", targets[0].getDestInfo().getSpool());
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void Given_Null_Feed_Then_Get_Targets_Returns_Empty_Array() {
        Target[] targets = nodeConfig.getTargets(null);
        targets[0].getDestInfo();
    }

    @Test(expected = ArrayIndexOutOfBoundsException.class)
    public void Given_Incorrect_Feed_Then_Get_Targets_Returns_Empty_Array() {
        Target[] targets = nodeConfig.getTargets("2");
        targets[0].getDestInfo();
    }

    @Test
    public void Given_Same_Ip_Then_Is_Another_Node_Returns_False() {
        Boolean isAnotherNode =
                nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OmtCTmhkWVFvbzhXNUphZ2g4T1N4Zmp6Mzl1ND0=", "172.0.0.1");
        Assert.assertFalse(isAnotherNode);
    }

    @Test
    public void Given_Different_Ip_Then_Is_Another_Node_Returns_True() {
        Boolean isAnotherNode =
                nodeConfig.isAnotherNode("Basic MTcyLjAuMC40OjlKOEFMUEhWQ2FpZ3FnZFpMMlRMYVRKSE1QQS8wNjdjR2JhV2RaUU1XSG1MNk5KbEtBVmpPbWtoZTR6ZmVlYjJzbElNMVR0REc2b0tYb0dLSDRMa1BBPT0=", "172.0.0.4");
        Assert.assertTrue(isAnotherNode);
    }

    @Test
    public void Given_Param_Name_Then_Get_Prov_Param_Returns_Parameter() {
        String paramValue = nodeConfig.getProvParam("DELIVERY_MAX_AGE");
        Assert.assertEquals("86400", paramValue);
    }

    @Test
    public void Validate_Get_All_Dests_Returns_Dest_Info() {
        DestInfo[] destInfo = nodeConfig.getAllDests();
        Assert.assertEquals("n:172.0.0.4", destInfo[0].getName());
    }

    @Test
    public void Validate_Get_MyAuth_Returns_Correct_Auth() {
        String auth = nodeConfig.getMyAuth();
        Assert.assertEquals("Basic TmFtZTo3YTRsQkxqMENQQ3lEbVVPaUI5Tks3b0pSeGROVUxqZzNNUHpkcEFYNHcvN09DNVR5S1hhWFA0MGR5aHhzbm90bXM4d1BGeXdHVlQ3MTJhcldXSFR1dz09", auth);
    }
}