use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestServiceTagProcessor method test5processReplace.
@Test
public void test5processReplace() throws Exception {
serviceTags = new ServiceTags();
serviceTags.setOp(ServiceTags.OP_REPLACE);
Map<Long, RangerTagDef> fd = new HashMap<>();
List<RangerTag> associatedTags = new ArrayList<>();
RangerTagDef rTagDef = Mockito.mock(RangerTagDef.class);
rTagDef.setGuid("guid");
rTagDef.setName("rTagDefName");
fd.put(1l, rTagDef);
serviceTags.setTagDefinitions(fd);
List<RangerServiceResource> serviceResources = new ArrayList<RangerServiceResource>();
RangerServiceResource rserRes = new RangerServiceResource();
testServiceRest = new TestServiceREST();
RangerPolicy rp = testServiceRest.rangerPolicy();
rserRes.setResourceElements(rp.getResources());
rserRes.setGuid("guId");
rserRes.setId(1L);
rserRes.setServiceName("serviceName1");
serviceResources.add(rserRes);
serviceTags.setServiceResources(serviceResources);
List<RangerTagResourceMap> tagResourceMaps = new ArrayList<RangerTagResourceMap>();
serviceTags.setServiceName("tagServiceName");
RangerTagResourceMap rangerTagRmp = new RangerTagResourceMap();
rangerTagRmp.setId(2L);
tagResourceMaps.add(rangerTagRmp);
Map<Long, List<Long>> resourceToTagIds = new HashMap<>();
resourceToTagIds.put(1L, new ArrayList<Long>(Arrays.asList(22L, 1L, 0L)));
serviceTags.setResourceToTagIds(resourceToTagIds);
RangerTag rTag = new RangerTag();
rTag.setId(22L);
rTag.setType("type1");
rTag.setGuid("tagGuID");
Map<Long, RangerTag> tags = new HashMap<>();
rTag.setOwner((short) 0);
tags.put(1L, rTag);
serviceTags.setTags(tags);
associatedTags.add(rTag);
TagStore tagStore = Mockito.mock(TagStore.class);
sTagProcessor = new ServiceTagsProcessor(tagStore);
List<String> serviceResourcesInDb = new ArrayList<>(Arrays.asList("guid"));
Mockito.when(tagStore.getServiceResourceGuidsByService(serviceTags.getServiceName())).thenReturn(serviceResourcesInDb);
Mockito.when(tagStore.getTagResourceMapsForResourceGuid(Mockito.anyString())).thenReturn(tagResourceMaps);
Mockito.doNothing().when(tagStore).deleteTagResourceMap(rangerTagRmp.getId());
Mockito.doNothing().when(tagStore).deleteServiceResourceByGuid(Mockito.anyString());
sTagProcessor.process(serviceTags);
Mockito.verify(tagStore).getServiceResourceGuidsByService(serviceTags.getServiceName());
Mockito.verify(tagStore).getTagResourceMapsForResourceGuid(Mockito.anyString());
Mockito.verify(tagStore).deleteTagResourceMap(rangerTagRmp.getId());
Mockito.verify(tagStore).deleteServiceResourceByGuid(Mockito.anyString());
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestServiceTagProcessor method test2processAddOrUpdate.
@Test
public void test2processAddOrUpdate() throws Exception {
serviceTags = new ServiceTags();
Map<Long, RangerTagDef> fd = new HashMap<>();
List<RangerTag> associatedTags = new ArrayList<>();
RangerTagDef rTagDef = Mockito.mock(RangerTagDef.class);
rTagDef.setGuid("guid");
rTagDef.setName("rTagDefname");
fd.put(1l, rTagDef);
serviceTags.setTagDefinitions(fd);
List<RangerServiceResource> serviceResources = new ArrayList<RangerServiceResource>();
RangerServiceResource rserRes = new RangerServiceResource();
testServiceRest = new TestServiceREST();
RangerPolicy rp = testServiceRest.rangerPolicy();
rserRes.setResourceElements(rp.getResources());
rserRes.setGuid("guId");
rserRes.setId(1L);
serviceResources.add(rserRes);
serviceTags.setServiceResources(serviceResources);
Map<Long, List<Long>> resourceToTagIds = new HashMap<>();
resourceToTagIds.put(1L, new ArrayList<Long>(Arrays.asList(11L, 1L, 13L)));
serviceTags.setResourceToTagIds(resourceToTagIds);
RangerTag rTag = new RangerTag();
rTag.setId(22L);
rTag.setOwner((short) 1);
Map<Long, RangerTag> tags = new HashMap<>();
tags.put(1L, rTag);
serviceTags.setTags(tags);
RangerTag rTag2 = new RangerTag();
rTag2.setId(22L);
rTag2.setOwner((short) 1);
Map<Long, RangerTag> tags2 = new HashMap<>();
tags2.put(1L, rTag2);
associatedTags.add(rTag2);
TagStore tagStore = Mockito.mock(TagStore.class);
sTagProcessor = new ServiceTagsProcessor(tagStore);
Mockito.when(tagStore.createTagDef(rTagDef)).thenReturn(rTagDef);
Mockito.when(tagStore.getServiceResourceByGuid(rserRes.getGuid())).thenReturn(rserRes);
Mockito.when(tagStore.createTag(rTag2)).thenReturn(rTag);
Mockito.when(tagStore.getTagsForResourceId(rserRes.getId())).thenReturn(associatedTags);
sTagProcessor.process(serviceTags);
Mockito.verify(tagStore).createTagDef(rTagDef);
Mockito.verify(tagStore).getServiceResourceByGuid(rserRes.getGuid());
Mockito.verify(tagStore).createTag(rTag2);
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class PatchForUpdatingTagsJson_J10020 method updateRangerTagsTablesWithTagsJson.
private void updateRangerTagsTablesWithTagsJson() throws Exception {
logger.info("==> updateRangerTagsTablesWithTagsJson() ");
List<RangerService> allServices = svcStore.getServices(new SearchFilter());
if (CollectionUtils.isNotEmpty(allServices)) {
TransactionTemplate txTemplate = new TransactionTemplate(txManager);
for (RangerService service : allServices) {
XXService dbService = daoMgr.getXXService().getById(service.getId());
RangerTagDBRetriever tagsRetriever = new RangerTagDBRetriever(daoMgr, txManager, dbService);
Map<Long, RangerTagDef> tagDefs = tagsRetriever.getTagDefs();
Map<Long, RangerTag> tags = tagsRetriever.getTags();
List<RangerServiceResource> serviceResources = tagsRetriever.getServiceResources();
XXTagDefDao tagDefDao = daoMgr.getXXTagDef();
XXTagDao tagDao = daoMgr.getXXTag();
XXServiceResourceDao serviceResourceDao = daoMgr.getXXServiceResource();
if (MapUtils.isNotEmpty(tagDefs)) {
logger.info("==> Port " + tagDefs.size() + " Tag Definitions for service(name=" + dbService.getName() + ")");
for (Map.Entry<Long, RangerTagDef> entry : tagDefs.entrySet()) {
RangerTagDef tagDef = entry.getValue();
XXTagDef xTagDef = tagDefDao.getById(tagDef.getId());
if (xTagDef != null && StringUtils.isEmpty(xTagDef.getTagAttrDefs())) {
TagsUpdaterThread updaterThread = new TagsUpdaterThread(txTemplate, null, null, tagDef);
String errorMsg = runThread(updaterThread);
if (StringUtils.isNotEmpty(errorMsg)) {
throw new Exception(errorMsg);
}
}
}
}
if (MapUtils.isNotEmpty(tags)) {
logger.info("==> Port " + tags.size() + " Tags for service(name=" + dbService.getName() + ")");
for (Map.Entry<Long, RangerTag> entry : tags.entrySet()) {
RangerTag tag = entry.getValue();
XXTag xTag = tagDao.getById(tag.getId());
if (xTag != null && StringUtils.isEmpty(xTag.getTagAttrs())) {
TagsUpdaterThread updaterThread = new TagsUpdaterThread(txTemplate, null, tag, null);
String errorMsg = runThread(updaterThread);
if (StringUtils.isNotEmpty(errorMsg)) {
throw new Exception(errorMsg);
}
}
}
}
if (CollectionUtils.isNotEmpty(serviceResources)) {
logger.info("==> Port " + serviceResources.size() + " Service Resources for service(name=" + dbService.getName() + ")");
for (RangerServiceResource serviceResource : serviceResources) {
XXServiceResource xServiceResource = serviceResourceDao.getById(serviceResource.getId());
if (xServiceResource != null && StringUtils.isEmpty(xServiceResource.getServiceResourceElements())) {
TagsUpdaterThread updaterThread = new TagsUpdaterThread(txTemplate, serviceResource, null, null);
String errorMsg = runThread(updaterThread);
if (StringUtils.isNotEmpty(errorMsg)) {
throw new Exception(errorMsg);
}
}
}
}
}
}
logger.info("<== updateRangerTagsTablesWithTagsJson() ");
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestTagREST method test4createTagDef.
@Test
public void test4createTagDef() {
RangerTagDef oldtagDef = new RangerTagDef();
oldtagDef.setId(id);
try {
Mockito.when(validator.preCreateTagDef(oldtagDef, false)).thenReturn(oldtagDef);
} catch (Exception e) {
}
Mockito.when(restErrorUtil.createRESTException(Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean())).thenThrow(new WebApplicationException());
thrown.expect(WebApplicationException.class);
tagREST.createTagDef(oldtagDef, false);
try {
Mockito.verify(validator).preCreateTagDef(oldtagDef, false);
} catch (Exception e) {
}
Mockito.verify(restErrorUtil).createRESTException(Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean());
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestTagREST method test2createTagDef.
@Test
public void test2createTagDef() {
RangerTagDef oldTagDef = new RangerTagDef();
RangerTagDef newTagDef = new RangerTagDef();
oldTagDef.setId(id);
newTagDef.setId(id);
newTagDef.setName(name);
try {
Mockito.when(validator.preCreateTagDef(oldTagDef, true)).thenReturn(oldTagDef);
} catch (Exception e) {
}
try {
Mockito.when(tagStore.updateTagDef(oldTagDef)).thenReturn(newTagDef);
} catch (Exception e) {
}
RangerTagDef rangerTagDef = tagREST.createTagDef(oldTagDef, true);
Assert.assertEquals(rangerTagDef.getName(), newTagDef.getName());
Assert.assertEquals(rangerTagDef.getId(), newTagDef.getId());
Assert.assertNotEquals(oldTagDef.getName(), rangerTagDef.getName());
try {
Mockito.verify(validator).preCreateTagDef(oldTagDef, true);
} catch (Exception e) {
}
try {
Mockito.verify(tagStore).updateTagDef(oldTagDef);
} catch (Exception e) {
}
}
Aggregations