private NameSpaceContainer validateEntity(String entityId) throws NamespaceException { final NameSpaceContainer entity = namespaceService.getEntityById(entityId); if (entity == null) { throw new IllegalArgumentException(String.format("Could not find entity with id [%s].", entityId)); } return entity; }
/** * Given an id, retrieves the entity from the namespace. Also handles fake ids (using generateInternalId) that we * generate for folders/files that exist in file-based sources that are not in the namespace. * * Note: this returns the namespace object (DatasetConfig, etc) for entites found in the namespace. For non-namespace * items it returns the appropriate CatalogEntity item (Folder/File only). */ private Optional<?> getById(String id) { try { if (isInternalId(id)) { Optional<CatalogItem> catalogItem = getInternalItemByPath(getPathFromInternalId(id)); if (!catalogItem.isPresent()) { return Optional.absent(); } return getCatalogEntityFromCatalogItem(catalogItem.get()); } else { Optional<?> optional = extractFromNamespaceContainer(namespaceService.getEntityById(id)); if (!optional.isPresent()) { logger.warn("Could not find entity with id [{}]", id); } return optional; } } catch (NamespaceException e) { logger.warn("Failed to get entity ", e); return Optional.absent(); } }
@Test public void testDeleteFolder() throws Exception { Folder folder = new Folder("folder-id", Arrays.asList("source", "folder"), "1", null); NameSpaceContainer namespaceContainer = new NameSpaceContainer(); namespaceContainer.setFolder(CatalogServiceHelper.getFolderConfig(folder)); namespaceContainer.setType(NameSpaceContainer.Type.FOLDER); when(namespaceService.getEntityById(folder.getId())).thenReturn(namespaceContainer); catalogServiceHelper.deleteCatalogItem(folder.getId(), folder.getTag()); verify(namespaceService, times(1)).deleteFolder(new NamespaceKey(folder.getPath()), folder.getTag()); }
@Test public void testDeleteSpace() throws Exception { Space space = new Space("space-id", "mySpace", "1", 0L, null); NameSpaceContainer namespaceContainer = new NameSpaceContainer(); namespaceContainer.setSpace(catalogServiceHelper.getSpaceConfig(space)); namespaceContainer.setType(NameSpaceContainer.Type.SPACE); when(namespaceService.getEntityById(space.getId())).thenReturn(namespaceContainer); catalogServiceHelper.deleteCatalogItem(space.getId(), space.getTag()); verify(namespaceService, times(1)).deleteSpace(new NamespaceKey(space.getName()), space.getTag()); }
@Test public void testGetDatasetCatalogEntityById() throws Exception { DatasetConfig datasetConfig = new DatasetConfig(); datasetConfig.setId(new EntityId("dataset-id")); datasetConfig.setFullPathList(Collections.singletonList("path")); datasetConfig.setType(VIRTUAL_DATASET); VirtualDataset virtualDataset = new VirtualDataset(); virtualDataset.setSql(""); datasetConfig.setVirtualDataset(virtualDataset); NameSpaceContainer namespaceContainer = new NameSpaceContainer(); namespaceContainer.setType(NameSpaceContainer.Type.DATASET); namespaceContainer.setDataset(datasetConfig); when(namespaceService.getEntityById(datasetConfig.getId().getId())).thenReturn(namespaceContainer); ReflectionSettings reflectionSettings = mock(ReflectionSettings.class); when(reflectionSettings.getStoredReflectionSettings(any(NamespaceKey.class))).thenReturn(Optional.<AccelerationSettings>absent()); when(reflectionServiceHelper.getReflectionSettings()).thenReturn(reflectionSettings); DremioTable dremioTable = mock(DremioTable.class); when(dremioTable.getDatasetConfig()).thenReturn(datasetConfig); when(catalog.getTable(any(String.class))).thenReturn(dremioTable); Optional<CatalogEntity> entity = catalogServiceHelper.getCatalogEntityById(datasetConfig.getId().getId()); assertTrue(entity.isPresent()); CatalogEntity catalogEntity = entity.get(); assertTrue(catalogEntity instanceof Dataset); Dataset dataset = (Dataset) catalogEntity; assertEquals(dataset.getId(), datasetConfig.getId().getId()); }
@Test public void testGetSpaceCatalogEntityById() throws Exception { SpaceConfig spaceConfig = new SpaceConfig(); spaceConfig.setId(new EntityId("space-id")); spaceConfig.setName("mySpace"); NameSpaceContainer namespaceContainer = new NameSpaceContainer(); namespaceContainer.setType(NameSpaceContainer.Type.SPACE); namespaceContainer.setSpace(spaceConfig); when(namespaceService.getEntityById(spaceConfig.getId().getId())).thenReturn(namespaceContainer); when(namespaceService.getEntities(Collections.singletonList(new NamespaceKey(spaceConfig.getName())))).thenReturn(Collections.singletonList(namespaceContainer)); // for children listing, we just send the space back to keep it simple when(namespaceService.list(new NamespaceKey(spaceConfig.getName()))).thenReturn(Collections.singletonList(namespaceContainer)); Optional<CatalogEntity> catalogEntityById = catalogServiceHelper.getCatalogEntityById(spaceConfig.getId().getId()); assertTrue(catalogEntityById.isPresent()); CatalogEntity catalogEntity = catalogEntityById.get(); assertTrue(catalogEntity instanceof Space); Space space = (Space) catalogEntity; assertEquals(space.getId(), spaceConfig.getId().getId()); assertEquals(space.getChildren().size(), 1); }
namespaceContainer.setType(NameSpaceContainer.Type.DATASET); namespaceContainer.setDataset(config); when(namespaceService.getEntityById(dataset.getId())).thenReturn(namespaceContainer);
@Test public void testDeleteDataset() throws Exception { Dataset dataset = new Dataset( "dataset-id", Dataset.DatasetType.VIRTUAL_DATASET, Arrays.asList("source", "path"), null, 0L, "1", null, "sql", null, null, null ); NameSpaceContainer namespaceContainer = new NameSpaceContainer(); namespaceContainer.setType(NameSpaceContainer.Type.DATASET); DatasetConfig datasetConfig = new DatasetConfig(); datasetConfig.setId(new EntityId(dataset.getId())); datasetConfig.setType(VIRTUAL_DATASET); datasetConfig.setFullPathList(dataset.getPath()); datasetConfig.setTag(dataset.getTag()); namespaceContainer.setDataset(datasetConfig); when(namespaceService.getEntityById(dataset.getId())).thenReturn(namespaceContainer); DremioTable dremioTable = mock(DremioTable.class); when(dremioTable.getDatasetConfig()).thenReturn(datasetConfig); when(catalog.getTable(any(String.class))).thenReturn(dremioTable); catalogServiceHelper.deleteCatalogItem(dataset.getId(), "1"); verify(namespaceService, times(1)).deleteDataset(new NamespaceKey(dataset.getPath()), datasetConfig.getTag()); }
namespaceContainer.setType(NameSpaceContainer.Type.DATASET); namespaceContainer.setDataset(config); when(namespaceService.getEntityById(dataset.getId())).thenReturn(namespaceContainer);
@Test public void testDeleteSource() throws Exception { SourceConfig sourceConfig = new SourceConfig(); sourceConfig.setName("mySource"); sourceConfig.setId(new EntityId("source-id")); sourceConfig.setTag("1"); sourceConfig.setType("NAS"); AccelerationSettings settings = new AccelerationSettings(); settings.setGracePeriod(1L); settings.setRefreshPeriod(1L); Source source = new Source(sourceConfig, settings, mock(ConnectionReader.class)); NameSpaceContainer namespaceContainer = new NameSpaceContainer(); namespaceContainer.setType(NameSpaceContainer.Type.SOURCE); namespaceContainer.setSource(sourceConfig); when(namespaceService.getEntityById(source.getId())).thenReturn(namespaceContainer); catalogServiceHelper.deleteCatalogItem(source.getId(), "1"); verify(sourceService, times(1)).deleteSource(sourceConfig); }