forked from apache/hive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestRangerLoadTask.java
379 lines (355 loc) · 23.1 KB
/
TestRangerLoadTask.java
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.hadoop.hive.ql.exec.repl;
import com.google.gson.Gson;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.ql.ErrorMsg;
import org.apache.hadoop.hive.ql.exec.repl.ranger.RangerExportPolicyList;
import org.apache.hadoop.hive.ql.exec.repl.ranger.RangerPolicy;
import org.apache.hadoop.hive.ql.exec.repl.ranger.RangerRestClientImpl;
import org.apache.hadoop.hive.ql.parse.repl.ReplState;
import org.apache.hadoop.hive.ql.parse.repl.metric.ReplicationMetricCollector;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.powermock.reflect.Whitebox;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import static org.apache.hadoop.hive.conf.HiveConf.ConfVars.REPL_RANGER_ADD_DENY_POLICY_TARGET;
import static org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils.RANGER_HIVE_SERVICE_NAME;
import static org.apache.hadoop.hive.ql.exec.repl.util.ReplUtils.RANGER_REST_URL;
/**
* Unit test class for testing Ranger Load.
*/
@RunWith(MockitoJUnitRunner.class)
public class TestRangerLoadTask {
protected static final Logger LOG = LoggerFactory.getLogger(TestRangerLoadTask.class);
private RangerLoadTask task;
@Mock
private RangerRestClientImpl mockClient;
@Mock
private HiveConf conf;
@Mock
private RangerLoadWork work;
@Mock
private ReplicationMetricCollector metricCollector;
@Before
public void setup() throws Exception {
task = new RangerLoadTask(mockClient, conf, work);
Mockito.when(mockClient.changeDataSet(Mockito.anyList(), Mockito.anyString(), Mockito.anyString()))
.thenCallRealMethod();
Mockito.when(mockClient.addDenyPolicies(Mockito.anyList(), Mockito.anyString(), Mockito.anyString(),
Mockito.anyString())).thenCallRealMethod();
Mockito.when(mockClient.checkConnection(Mockito.anyString(), Mockito.any())).thenReturn(true);
Mockito.when(work.getMetricCollector()).thenReturn(metricCollector);
}
@Test
public void testFailureInvalidAuthProviderEndpoint() {
Mockito.when(work.getCurrentDumpPath()).thenReturn(new Path("dumppath"));
int status = task.execute();
Assert.assertEquals(ErrorMsg.REPL_INVALID_CONFIG_FOR_SERVICE.getErrorCode(), status);
}
@Test
public void testSuccessValidAuthProviderEndpoint() throws MalformedURLException {
Mockito.when(conf.get(RANGER_REST_URL)).thenReturn("rangerEndpoint");
Mockito.when(work.getSourceDbName()).thenReturn("srcdb");
Mockito.when(work.getTargetDbName()).thenReturn("tgtdb");
Mockito.when(work.getRangerConfigResource()).thenReturn(new URL("file://ranger.xml"));
int status = task.execute();
Assert.assertEquals(0, status);
}
@Test
public void testSuccessNonEmptyRangerPolicies() throws Exception {
String rangerResponse = "{\"metaDataInfo\":{\"Host name\":\"ranger.apache.org\","
+ "\"Exported by\":\"hive\",\"Export time\":\"May 5, 2020, 8:55:03 AM\",\"Ranger apache version\""
+ ":\"2.0.0.7.2.0.0-61\"},\"policies\":[{\"service\":\"hive\",\"name\":\"db-level\",\"policyType\":0,"
+ "\"description\":\"\",\"isAuditEnabled\":true,\"resources\":{\"database\":{\"values\":[\"aa\"],"
+ "\"isExcludes\":false,\"isRecursive\":false},\"column\":{\"values\":[\"id\"],\"isExcludes\":false,"
+ "\"isRecursive\":false},\"table\":{\"values\":[\"*\"],\"isExcludes\":false,\"isRecursive\":false}},"
+ "\"policyItems\":[{\"accesses\":[{\"type\":\"select\",\"isAllowed\":true},{\"type\":\"update\","
+ "\"isAllowed\":true}],\"users\":[\"admin\"],\"groups\":[\"public\"],\"conditions\":[],"
+ "\"delegateAdmin\":false}],\"denyPolicyItems\":[],\"allowExceptions\":[],\"denyExceptions\":[],"
+ "\"dataMaskPolicyItems\":[],\"rowFilterPolicyItems\":[],\"id\":40,\"guid\":"
+ "\"4e2b3406-7b9a-4004-8cdf-7a239c8e2cae\",\"isEnabled\":true,\"version\":1}]}";
RangerExportPolicyList rangerPolicyList = new Gson().fromJson(rangerResponse, RangerExportPolicyList.class);
Mockito.when(conf.get(RANGER_REST_URL)).thenReturn("rangerEndpoint");
Mockito.when(work.getSourceDbName()).thenReturn("srcdb");
Mockito.when(work.getTargetDbName()).thenReturn("tgtdb");
Path rangerDumpPath = new Path("/tmp");
Mockito.when(work.getCurrentDumpPath()).thenReturn(rangerDumpPath);
Mockito.when(mockClient.readRangerPoliciesFromJsonFile(Mockito.any(), Mockito.any())).thenReturn(rangerPolicyList);
Mockito.when(work.getRangerConfigResource()).thenReturn(new URL("file://ranger.xml"));
int status = task.execute();
Assert.assertEquals(0, status);
}
@Test
public void testSuccessRangerDumpMetrics() throws Exception {
Logger logger = Mockito.mock(Logger.class);
Whitebox.setInternalState(ReplState.class, logger);
String rangerResponse = "{\"metaDataInfo\":{\"Host name\":\"ranger.apache.org\","
+ "\"Exported by\":\"hive\",\"Export time\":\"May 5, 2020, 8:55:03 AM\",\"Ranger apache version\""
+ ":\"2.0.0.7.2.0.0-61\"},\"policies\":[{\"service\":\"cm_hive\",\"name\":\"db-level\",\"policyType\":0,"
+ "\"description\":\"\",\"isAuditEnabled\":true,\"resources\":{\"database\":{\"values\":[\"aa\"],"
+ "\"isExcludes\":false,\"isRecursive\":false},\"column\":{\"values\":[\"id\"],\"isExcludes\":false,"
+ "\"isRecursive\":false},\"table\":{\"values\":[\"*\"],\"isExcludes\":false,\"isRecursive\":false}},"
+ "\"policyItems\":[{\"accesses\":[{\"type\":\"select\",\"isAllowed\":true},{\"type\":\"update\","
+ "\"isAllowed\":true}],\"users\":[\"admin\"],\"groups\":[\"public\"],\"conditions\":[],"
+ "\"delegateAdmin\":false}],\"denyPolicyItems\":[],\"allowExceptions\":[],\"denyExceptions\":[],"
+ "\"dataMaskPolicyItems\":[],\"rowFilterPolicyItems\":[],\"id\":40,\"guid\":"
+ "\"4e2b3406-7b9a-4004-8cdf-7a239c8e2cae\",\"isEnabled\":true,\"version\":1}]}";
RangerExportPolicyList rangerPolicyList = new Gson().fromJson(rangerResponse, RangerExportPolicyList.class);
Mockito.when(conf.get(RANGER_REST_URL)).thenReturn("rangerEndpoint");
Mockito.when(work.getSourceDbName()).thenReturn("srcdb");
Mockito.when(work.getTargetDbName()).thenReturn("tgtdb");
Path rangerDumpPath = new Path("/tmp");
Mockito.when(work.getCurrentDumpPath()).thenReturn(rangerDumpPath);
Mockito.when(mockClient.readRangerPoliciesFromJsonFile(Mockito.any(), Mockito.any())).thenReturn(rangerPolicyList);
Mockito.when(work.getRangerConfigResource()).thenReturn(new URL("file://ranger.xml"));
int status = task.execute();
Assert.assertEquals(0, status);
ArgumentCaptor<String> replStateCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Object> eventCaptor = ArgumentCaptor.forClass(Object.class);
ArgumentCaptor<Object> eventDetailsCaptor = ArgumentCaptor.forClass(Object.class);
Mockito.verify(logger,
Mockito.times(2)).info(replStateCaptor.capture(),
eventCaptor.capture(), eventDetailsCaptor.capture());
Assert.assertEquals("REPL::{}: {}", replStateCaptor.getAllValues().get(0));
Assert.assertEquals("RANGER_LOAD_START", eventCaptor.getAllValues().get(0));
Assert.assertEquals("RANGER_LOAD_END", eventCaptor.getAllValues().get(1));
Assert.assertTrue(eventDetailsCaptor.getAllValues().get(0)
.toString().contains("{\"sourceDbName\":\"srcdb\",\"targetDbName\":\"tgtdb\""
+ ",\"estimatedNumPolicies\":1,\"loadStartTime\":"));
Assert.assertTrue(eventDetailsCaptor
.getAllValues().get(1).toString().contains("{\"sourceDbName\":\"srcdb\",\"targetDbName\""
+ ":\"tgtdb\",\"actualNumPolicies\":1,\"loadEndTime\""));
}
@Test
public void testSuccessAddDenyRangerPolicies() throws Exception {
String rangerResponse = "{\"metaDataInfo\":{\"Host name\":\"ranger.apache.org\","
+ "\"Exported by\":\"hive\",\"Export time\":\"May 5, 2020, 8:55:03 AM\",\"Ranger apache version\""
+ ":\"2.0.0.7.2.0.0-61\"},\"policies\":[{\"service\":\"hive\",\"name\":\"db-level\",\"policyType\":0,"
+ "\"description\":\"\",\"isAuditEnabled\":true,\"resources\":{\"database\":{\"values\":[\"aa\"],"
+ "\"isExcludes\":false,\"isRecursive\":false},\"column\":{\"values\":[\"id\"],\"isExcludes\":false,"
+ "\"isRecursive\":false},\"table\":{\"values\":[\"*\"],\"isExcludes\":false,\"isRecursive\":false}},"
+ "\"policyItems\":[{\"accesses\":[{\"type\":\"select\",\"isAllowed\":true},{\"type\":\"update\","
+ "\"isAllowed\":true}],\"users\":[\"admin\"],\"groups\":[\"public\"],\"conditions\":[],"
+ "\"delegateAdmin\":false}],\"denyPolicyItems\":[],\"allowExceptions\":[],\"denyExceptions\":[],"
+ "\"dataMaskPolicyItems\":[],\"rowFilterPolicyItems\":[],\"id\":40,\"guid\":"
+ "\"4e2b3406-7b9a-4004-8cdf-7a239c8e2cae\",\"isEnabled\":true,\"version\":1}]}";
RangerExportPolicyList rangerPolicyList = new Gson().fromJson(rangerResponse, RangerExportPolicyList.class);
Mockito.when(conf.get(RANGER_REST_URL)).thenReturn("rangerEndpoint");
Mockito.when(work.getSourceDbName()).thenReturn("srcdb");
Mockito.when(work.getTargetDbName()).thenReturn("tgtdb");
Mockito.when(conf.get(RANGER_HIVE_SERVICE_NAME)).thenReturn("hive");
Mockito.when(conf.getBoolVar(REPL_RANGER_ADD_DENY_POLICY_TARGET)).thenReturn(true);
Path rangerDumpPath = new Path("/tmp");
Mockito.when(work.getCurrentDumpPath()).thenReturn(rangerDumpPath);
Mockito.when(mockClient.readRangerPoliciesFromJsonFile(Mockito.any(), Mockito.any())).thenReturn(rangerPolicyList);
Mockito.when(work.getRangerConfigResource()).thenReturn(new URL("file://ranger.xml"));
int status = task.execute();
Assert.assertEquals(0, status);
ArgumentCaptor<RangerExportPolicyList> rangerPolicyCapture = ArgumentCaptor.forClass(RangerExportPolicyList.class);
ArgumentCaptor<String> rangerEndpoint = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> serviceName = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> targetDb = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<HiveConf> confCaptor = ArgumentCaptor.forClass(HiveConf.class);
Mockito.verify(mockClient,
Mockito.times(1)).importRangerPolicies(rangerPolicyCapture.capture(),
targetDb.capture(), rangerEndpoint.capture(), serviceName.capture(), confCaptor.capture());
Assert.assertEquals("tgtdb", targetDb.getAllValues().get(0));
Assert.assertEquals("rangerEndpoint", rangerEndpoint.getAllValues().get(0));
Assert.assertEquals("hive", serviceName.getAllValues().get(0));
RangerExportPolicyList actualPolicyList = rangerPolicyCapture.getAllValues().get(0);
Assert.assertEquals(rangerPolicyList.getMetaDataInfo(), actualPolicyList.getMetaDataInfo());
//Deny policy is added
Assert.assertEquals(2, actualPolicyList.getListSize());
RangerPolicy denyPolicy = actualPolicyList.getPolicies().get(1);
Assert.assertEquals("hive", denyPolicy.getService());
Assert.assertEquals("srcdb_replication deny policy for tgtdb", denyPolicy.getName());
Assert.assertEquals(1, denyPolicy.getDenyExceptions().size());
Assert.assertEquals("public", denyPolicy.getDenyPolicyItems().get(0).getGroups().get(0));
Assert.assertEquals(8, denyPolicy.getDenyPolicyItems().get(0).getAccesses().size());
boolean isReplAdminDenied = false;
for (RangerPolicy.RangerPolicyItemAccess access : denyPolicy.getDenyPolicyItems().get(0).getAccesses()) {
if (access.getType().equalsIgnoreCase("ReplAdmin")) {
isReplAdminDenied = true;
}
}
Assert.assertTrue(isReplAdminDenied);
//Deny exception is for hive user. Deny exception is not for repl admin permission
Assert.assertEquals("hive", denyPolicy.getDenyExceptions().get(0).getUsers().get(0));
Assert.assertEquals(10, denyPolicy.getDenyExceptions().get(0).getAccesses().size());
isReplAdminDenied = false;
for (RangerPolicy.RangerPolicyItemAccess access : denyPolicy.getDenyExceptions().get(0).getAccesses()) {
if (access.getType().equalsIgnoreCase("ReplAdmin")) {
isReplAdminDenied = true;
}
}
Assert.assertTrue(isReplAdminDenied);
}
@Test
public void testSuccessDisableDenyRangerPolicies() throws Exception {
String rangerResponse = "{\"metaDataInfo\":{\"Host name\":\"ranger.apache.org\","
+ "\"Exported by\":\"hive\",\"Export time\":\"May 5, 2020, 8:55:03 AM\",\"Ranger apache version\""
+ ":\"2.0.0.7.2.0.0-61\"},\"policies\":[{\"service\":\"hive\",\"name\":\"db-level\",\"policyType\":0,"
+ "\"description\":\"\",\"isAuditEnabled\":true,\"resources\":{\"database\":{\"values\":[\"aa\"],"
+ "\"isExcludes\":false,\"isRecursive\":false},\"column\":{\"values\":[\"id\"],\"isExcludes\":false,"
+ "\"isRecursive\":false},\"table\":{\"values\":[\"*\"],\"isExcludes\":false,\"isRecursive\":false}},"
+ "\"policyItems\":[{\"accesses\":[{\"type\":\"select\",\"isAllowed\":true},{\"type\":\"update\","
+ "\"isAllowed\":true}],\"users\":[\"admin\"],\"groups\":[\"public\"],\"conditions\":[],"
+ "\"delegateAdmin\":false}],\"denyPolicyItems\":[],\"allowExceptions\":[],\"denyExceptions\":[],"
+ "\"dataMaskPolicyItems\":[],\"rowFilterPolicyItems\":[],\"id\":40,\"guid\":"
+ "\"4e2b3406-7b9a-4004-8cdf-7a239c8e2cae\",\"isEnabled\":true,\"version\":1}]}";
RangerExportPolicyList rangerPolicyList = new Gson().fromJson(rangerResponse, RangerExportPolicyList.class);
Mockito.when(conf.get(RANGER_REST_URL)).thenReturn("rangerEndpoint");
Mockito.when(work.getSourceDbName()).thenReturn("srcdb");
Mockito.when(work.getTargetDbName()).thenReturn("tgtdb");
Mockito.when(conf.get(RANGER_HIVE_SERVICE_NAME)).thenReturn("hive");
Mockito.when(conf.getBoolVar(REPL_RANGER_ADD_DENY_POLICY_TARGET)).thenReturn(false);
Path rangerDumpPath = new Path("/tmp");
Mockito.when(work.getCurrentDumpPath()).thenReturn(rangerDumpPath);
Mockito.when(mockClient.readRangerPoliciesFromJsonFile(Mockito.any(), Mockito.any())).thenReturn(rangerPolicyList);
Mockito.when(work.getRangerConfigResource()).thenReturn(new URL("file://ranger.xml"));
int status = task.execute();
Assert.assertEquals(0, status);
ArgumentCaptor<RangerExportPolicyList> rangerPolicyCapture = ArgumentCaptor.forClass(RangerExportPolicyList.class);
ArgumentCaptor<String> rangerEndpoint = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> serviceName = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> targetDb = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<HiveConf> confCaptor = ArgumentCaptor.forClass(HiveConf.class);
Mockito.verify(mockClient,
Mockito.times(1)).importRangerPolicies(rangerPolicyCapture.capture(),
targetDb.capture(), rangerEndpoint.capture(), serviceName.capture(), confCaptor.capture());
Assert.assertEquals("tgtdb", targetDb.getAllValues().get(0));
Assert.assertEquals("rangerEndpoint", rangerEndpoint.getAllValues().get(0));
Assert.assertEquals("hive", serviceName.getAllValues().get(0));
RangerExportPolicyList actualPolicyList = rangerPolicyCapture.getAllValues().get(0);
Assert.assertEquals(rangerPolicyList.getMetaDataInfo(), actualPolicyList.getMetaDataInfo());
//Deny policy is added
Assert.assertEquals(1, actualPolicyList.getListSize());
}
@Test
public void testRangerEndpointCreation() throws Exception {
RangerRestClientImpl rangerRestClient = new RangerRestClientImpl();
Assert.assertTrue(rangerRestClient.getRangerExportUrl("http://ranger.apache.org:6080",
"hive", "dbname").equals("http://ranger.apache.org:6080/service/plugins/"
+ "policies/exportJson?serviceName=hive&polResource=dbname&resource%3Adatabase=dbname&serviceType=hive"
+ "&resourceMatchScope=self_or_ancestor&resourceMatch=full"));
Assert.assertTrue(rangerRestClient.getRangerExportUrl("http://ranger.apache.org:6080/",
"hive", "dbname").equals("http://ranger.apache.org:6080/service/plugins/"
+ "policies/exportJson?serviceName=hive&polResource=dbname&resource%3Adatabase=dbname&serviceType=hive"
+ "&resourceMatchScope=self_or_ancestor&resourceMatch=full"));
Assert.assertTrue(rangerRestClient.getRangerImportUrl("http://ranger.apache.org:6080/",
"dbname").equals("http://ranger.apache.org:6080/service/plugins/policies/importPoliciesFromFile"
+ "?mergeIfExists=true&polResource=dbname"));
Assert.assertTrue(rangerRestClient.getRangerImportUrl("http://ranger.apache.org:6080",
"dbname").equals("http://ranger.apache.org:6080/service/plugins/policies/importPoliciesFromFile"
+ "?mergeIfExists=true&polResource=dbname"));
}
@Test
public void testChangeDataSet() throws Exception {
RangerRestClientImpl rangerRestClient = new RangerRestClientImpl();
String rangerResponse = "{\"metaDataInfo\":{\"Host name\":\"ranger.apache.org\","
+ "\"Exported by\":\"hive\",\"Export time\":\"May 5, 2020, 8:55:03 AM\",\"Ranger apache version\""
+ ":\"2.0.0.7.2.0.0-61\"},\"policies\":[{\"service\":\"cm_hive\",\"name\":\"db-level\",\"policyType\":0,"
+ "\"description\":\"\",\"isAuditEnabled\":true,\"resources\":{\"database\":{\"values\":[\"aa\"],"
+ "\"isExcludes\":false,\"isRecursive\":false},\"column\":{\"values\":[\"id\"],\"isExcludes\":false,"
+ "\"isRecursive\":false},\"table\":{\"values\":[\"*\"],\"isExcludes\":false,\"isRecursive\":false}},"
+ "\"policyItems\":[{\"accesses\":[{\"type\":\"select\",\"isAllowed\":true},{\"type\":\"update\","
+ "\"isAllowed\":true}],\"users\":[\"admin\"],\"groups\":[\"public\"],\"conditions\":[],"
+ "\"delegateAdmin\":false}],\"denyPolicyItems\":[],\"allowExceptions\":[],\"denyExceptions\":[],"
+ "\"dataMaskPolicyItems\":[],\"rowFilterPolicyItems\":[],\"id\":40,\"guid\":"
+ "\"4e2b3406-7b9a-4004-8cdf-7a239c8e2cae\",\"isEnabled\":true,\"version\":1}]}";
RangerExportPolicyList rangerPolicyList = new Gson().fromJson(rangerResponse, RangerExportPolicyList.class);
List<RangerPolicy> rangerPolicies = rangerPolicyList.getPolicies();
rangerRestClient.changeDataSet(rangerPolicies, null, null);
assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies,
null, null), "aa");
assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies,
"aa", null), "aa");
assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies,
null, "aa"), "aa");
assertEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies,
"aa", "aa"), "aa");
assertNotEqualsRangerPolicies(rangerPolicies, rangerRestClient.changeDataSet(rangerPolicies,
"aa", "tgt_aa"), "tgt_aa");
}
private void assertNotEqualsRangerPolicies(List<RangerPolicy> expectedRangerPolicies,
List<RangerPolicy> actualRangerPolicies, String targetName) {
Assert.assertEquals(expectedRangerPolicies.size(), actualRangerPolicies.size());
for (int index = 0; index < expectedRangerPolicies.size(); index++) {
Assert.assertEquals(expectedRangerPolicies.get(index).getName(), actualRangerPolicies.get(index).getName());
Assert.assertEquals(expectedRangerPolicies.get(index).getService(), actualRangerPolicies.get(index).getService());
Assert.assertEquals(expectedRangerPolicies.get(index).getDescription(),
actualRangerPolicies.get(index).getDescription());
Assert.assertEquals(expectedRangerPolicies.get(index).getPolicyType(),
actualRangerPolicies.get(index).getPolicyType());
Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(),
actualRangerPolicies.get(index).getResources().size());
Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(),
actualRangerPolicies.get(index).getResources().size());
RangerPolicy.RangerPolicyResource expectedRangerPolicyResource = expectedRangerPolicies.get(index)
.getResources().get("database");
RangerPolicy.RangerPolicyResource actualRangerPolicyResource = actualRangerPolicies.get(index)
.getResources().get("database");
Assert.assertEquals(expectedRangerPolicyResource.getValues().size(),
actualRangerPolicyResource.getValues().size());
for (int resourceIndex = 0; resourceIndex < expectedRangerPolicyResource.getValues().size(); resourceIndex++) {
Assert.assertEquals(actualRangerPolicyResource.getValues().get(index),
targetName);
}
}
}
private void assertEqualsRangerPolicies(List<RangerPolicy> expectedRangerPolicies,
List<RangerPolicy> actualRangerPolicies, String sourceName) {
Assert.assertEquals(expectedRangerPolicies.size(), actualRangerPolicies.size());
for (int index = 0; index < expectedRangerPolicies.size(); index++) {
Assert.assertEquals(expectedRangerPolicies.get(index).getName(), actualRangerPolicies.get(index).getName());
Assert.assertEquals(expectedRangerPolicies.get(index).getService(), actualRangerPolicies.get(index).getService());
Assert.assertEquals(expectedRangerPolicies.get(index).getDescription(),
actualRangerPolicies.get(index).getDescription());
Assert.assertEquals(expectedRangerPolicies.get(index).getPolicyType(),
actualRangerPolicies.get(index).getPolicyType());
Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(),
actualRangerPolicies.get(index).getResources().size());
Assert.assertEquals(expectedRangerPolicies.get(index).getResources().size(),
actualRangerPolicies.get(index).getResources().size());
RangerPolicy.RangerPolicyResource expectedRangerPolicyResource = expectedRangerPolicies.get(index)
.getResources().get("database");
RangerPolicy.RangerPolicyResource actualRangerPolicyResource = actualRangerPolicies.get(index)
.getResources().get("database");
Assert.assertEquals(expectedRangerPolicyResource.getValues().size(),
actualRangerPolicyResource.getValues().size());
for (int resourceIndex = 0; resourceIndex < expectedRangerPolicyResource.getValues().size(); resourceIndex++) {
Assert.assertEquals(expectedRangerPolicyResource.getValues().get(index),
actualRangerPolicyResource.getValues().get(index));
Assert.assertEquals(actualRangerPolicyResource.getValues().get(index),
sourceName);
}
}
}
}