Search in sources :

Example 21 with RangerTagDef

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());
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ArrayList(java.util.ArrayList) TagStore(org.apache.ranger.plugin.store.TagStore) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) RangerTagResourceMap(org.apache.ranger.plugin.model.RangerTagResourceMap) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 22 with RangerTagDef

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);
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ArrayList(java.util.ArrayList) TagStore(org.apache.ranger.plugin.store.TagStore) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 23 with RangerTagDef

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() ");
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) XXTagDef(org.apache.ranger.entity.XXTagDef) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) SearchFilter(org.apache.ranger.plugin.util.SearchFilter) XXTagDao(org.apache.ranger.db.XXTagDao) RangerTag(org.apache.ranger.plugin.model.RangerTag) RangerService(org.apache.ranger.plugin.model.RangerService) XXServiceResourceDao(org.apache.ranger.db.XXServiceResourceDao) XXServiceResource(org.apache.ranger.entity.XXServiceResource) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) XXTagDefDao(org.apache.ranger.db.XXTagDefDao) XXTag(org.apache.ranger.entity.XXTag) XXService(org.apache.ranger.entity.XXService) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with RangerTagDef

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());
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) WebApplicationException(javax.ws.rs.WebApplicationException) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 25 with RangerTagDef

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) {
    }
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Aggregations

RangerTagDef (org.apache.ranger.plugin.model.RangerTagDef)39 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)15 XXTagDef (org.apache.ranger.entity.XXTagDef)14 HashMap (java.util.HashMap)10 RangerTag (org.apache.ranger.plugin.model.RangerTag)10 WebApplicationException (javax.ws.rs.WebApplicationException)9 RangerServiceResource (org.apache.ranger.plugin.model.RangerServiceResource)9 ExpectedException (org.junit.rules.ExpectedException)9 XXTagDefDao (org.apache.ranger.db.XXTagDefDao)7 ServiceTags (org.apache.ranger.plugin.util.ServiceTags)6 List (java.util.List)5 Map (java.util.Map)5 XXTagAttributeDef (org.apache.ranger.entity.XXTagAttributeDef)5 Date (java.util.Date)4 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)4 RangerTagResourceMap (org.apache.ranger.plugin.model.RangerTagResourceMap)4 TagStore (org.apache.ranger.plugin.store.TagStore)4 Predicate (org.apache.commons.collections.Predicate)3 RangerServiceResourceSignature (org.apache.ranger.plugin.store.RangerServiceResourceSignature)3