Search in sources :

Example 1 with MetaStorage

use of com.bakdata.conquery.io.storage.MetaStorage in project conquery by bakdata.

the class GroupHandlingTest method execute.

@Override
public void execute(StandaloneSupport conquery) throws Exception {
    MetaStorage storage = conquery.getMetaStorage();
    Group group1 = new Group("company", "company", storage);
    User user1 = new User("user", "user", storage);
    User user1copy = new User("user", "user", storage);
    User user2 = new User("user2", "user2", storage);
    try {
        storage.addGroup(group1);
        storage.addUser(user1);
        group1.addMember(user1);
        group1.addMember(user1copy);
        assertThat(group1.getMembers()).containsExactlyInAnyOrder(user1.getId());
        group1.addMember(user2);
        assertThat(group1.getMembers()).containsExactlyInAnyOrder(user1.getId(), user2.getId());
        group1.removeMember(user2);
        assertThat(group1.getMembers()).containsExactlyInAnyOrder(user1.getId());
    } finally {
        storage.removeUser(user1.getId());
        storage.removeUser(user2.getId());
        storage.removeGroup(group1.getId());
    }
}
Also used : Group(com.bakdata.conquery.models.auth.entities.Group) User(com.bakdata.conquery.models.auth.entities.User) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage)

Example 2 with MetaStorage

use of com.bakdata.conquery.io.storage.MetaStorage in project conquery by bakdata.

the class ImportUpdateTest method execute.

@Override
public void execute(String name, TestConquery testConquery) throws Exception {
    final StandaloneSupport conquery = testConquery.getSupport(name);
    MetaStorage storage = conquery.getMetaStorage();
    String testJson = In.resource("/tests/query/UPDATE_IMPORT_TESTS/SIMPLE_TREECONCEPT_Query.json").withUTF8().readAll();
    final Dataset dataset = conquery.getDataset();
    final Namespace namespace = conquery.getNamespace();
    final ImportId importId1 = ImportId.Parser.INSTANCE.parse(dataset.getName(), "table1", "table1");
    final ImportId importId2 = ImportId.Parser.INSTANCE.parse(dataset.getName(), "table2", "table2");
    QueryTest test = (QueryTest) JsonIntegrationTest.readJson(dataset, testJson);
    final List<RequiredTable> tables = test.getContent().getTables();
    assertThat(tables.size()).isEqualTo(2);
    List<File> cqpps;
    // Manually import data, so we can do our own work.
    {
        ValidatorHelper.failOnError(log, conquery.getValidator().validate(test));
        importSecondaryIds(conquery, test.getContent().getSecondaryIds());
        conquery.waitUntilWorkDone();
        LoadingUtil.importTables(conquery, tables);
        conquery.waitUntilWorkDone();
        LoadingUtil.importConcepts(conquery, test.getRawConcepts());
        conquery.waitUntilWorkDone();
        cqpps = LoadingUtil.generateCqpp(conquery, tables);
        conquery.waitUntilWorkDone();
        assertThat(cqpps.size()).isEqualTo(tables.size());
        LoadingUtil.importCqppFiles(conquery, List.of(cqpps.get(0)));
        conquery.waitUntilWorkDone();
    }
    final Query query = IntegrationUtils.parseQuery(conquery, test.getRawQuery());
    // State before update.
    {
        log.info("Checking state before update");
        assertThat(namespace.getStorage().getAllImports()).hasSize(1);
        // Must contain the import.
        assertThat(namespace.getStorage().getAllImports()).filteredOn(imp -> imp.getId().equals(importId1)).isNotEmpty();
        assertThat(namespace.getStorage().getCentralRegistry().getOptional(importId1)).isNotEmpty();
        for (ShardNode node : conquery.getShardNodes()) {
            for (Worker worker : node.getWorkers().getWorkers().values()) {
                if (!worker.getInfo().getDataset().equals(dataset.getId())) {
                    continue;
                }
                final ModificationShieldedWorkerStorage workerStorage = worker.getStorage();
                assertThat(workerStorage.getAllCBlocks()).describedAs("CBlocks for Worker %s", worker.getInfo().getId()).filteredOn(block -> block.getBucket().getId().getDataset().equals(dataset.getId())).isNotEmpty();
                assertThat(workerStorage.getAllBuckets()).filteredOn(bucket -> bucket.getId().getDataset().equals(dataset.getId())).describedAs("Buckets for Worker %s", worker.getInfo().getId()).isNotEmpty();
                // Must contain the import.
                assertThat(workerStorage.getImport(importId1)).isNotNull();
            }
        }
        assertThat(namespace.getNumberOfEntities()).isEqualTo(4);
        // assert that the query can be executed after the import
        IntegrationUtils.assertQueryResult(conquery, query, 2L, ExecutionState.DONE, conquery.getTestUser(), 201);
    }
    // Try to update an import that does not exist should throw a Not-Found Webapplication Exception
    LoadingUtil.updateCqppFile(conquery, cqpps.get(1), Response.Status.Family.CLIENT_ERROR, "Not Found");
    conquery.waitUntilWorkDone();
    // Load manually new data for import and update the concerned import
    {
        log.info("Manually loading new data for import");
        final RequiredTable importTable = test.getContent().getTables().stream().filter(table -> table.getName().equalsIgnoreCase(importId1.getTable().getTable())).findFirst().orElseThrow();
        final String csvName = importTable.getCsv().getName();
        final String path = importTable.getCsv().getPath();
        // copy new content of the importTable into the csv-File used by the preprocessor to avoid creating multiple files withe same names
        FileUtils.copyInputStreamToFile(In.resource(path.substring(0, path.lastIndexOf('/')) + "/" + csvName.replace(".csv", ".update.csv")).asStream(), new File(conquery.getTmpDir(), csvName));
        File descriptionFile = new File(conquery.getTmpDir(), importTable.getName() + ConqueryConstants.EXTENSION_DESCRIPTION);
        File newPreprocessedFile = new File(conquery.getTmpDir(), importTable.getName() + ConqueryConstants.EXTENSION_PREPROCESSED);
        // create import descriptor
        {
            TableImportDescriptor desc = new TableImportDescriptor();
            desc.setName(importTable.getName());
            desc.setTable(importTable.getName());
            TableInputDescriptor input = new TableInputDescriptor();
            {
                input.setPrimary(importTable.getPrimaryColumn().createOutput());
                input.setSourceFile(csvName);
                input.setOutput(new OutputDescription[importTable.getColumns().length]);
                for (int i = 0; i < importTable.getColumns().length; i++) {
                    input.getOutput()[i] = importTable.getColumns()[i].createOutput();
                }
            }
            desc.setInputs(new TableInputDescriptor[] { input });
            Jackson.MAPPER.writeValue(descriptionFile, desc);
        }
        // preprocess
        conquery.preprocessTmp(conquery.getTmpDir(), List.of(descriptionFile));
        log.info("updating import");
        // correct update of the import
        LoadingUtil.updateCqppFile(conquery, newPreprocessedFile, Response.Status.Family.SUCCESSFUL, "No Content");
        conquery.waitUntilWorkDone();
    }
    // State after update.
    {
        log.info("Checking state after update");
        assertThat(namespace.getStorage().getAllImports()).hasSize(1);
        // Must contain the import.
        assertThat(namespace.getStorage().getAllImports()).filteredOn(imp -> imp.getId().equals(importId1)).isNotEmpty();
        assertThat(namespace.getStorage().getCentralRegistry().getOptional(importId1)).isNotEmpty();
        for (ShardNode node : conquery.getShardNodes()) {
            for (Worker worker : node.getWorkers().getWorkers().values()) {
                if (!worker.getInfo().getDataset().equals(dataset.getId())) {
                    continue;
                }
                final ModificationShieldedWorkerStorage workerStorage = worker.getStorage();
                assertThat(workerStorage.getAllCBlocks()).describedAs("CBlocks for Worker %s", worker.getInfo().getId()).filteredOn(block -> block.getBucket().getId().getDataset().equals(dataset.getId())).isNotEmpty();
                assertThat(workerStorage.getAllBuckets()).filteredOn(bucket -> bucket.getId().getDataset().equals(dataset.getId())).describedAs("Buckets for Worker %s", worker.getInfo().getId()).isNotEmpty();
                // Must contain the import.
                assertThat(workerStorage.getImport(importId1)).isNotNull();
            }
        }
        assertThat(namespace.getNumberOfEntities()).isEqualTo(9);
        // Issue a query and assert that it has more content.
        IntegrationUtils.assertQueryResult(conquery, query, 4L, ExecutionState.DONE, conquery.getTestUser(), 201);
    }
}
Also used : ExecutionState(com.bakdata.conquery.models.execution.ExecutionState) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RequiredTable(com.bakdata.conquery.integration.common.RequiredTable) QueryTest(com.bakdata.conquery.integration.json.QueryTest) Worker(com.bakdata.conquery.models.worker.Worker) ModificationShieldedWorkerStorage(com.bakdata.conquery.io.storage.ModificationShieldedWorkerStorage) In(com.github.powerlibraries.io.In) LoadingUtil.importSecondaryIds(com.bakdata.conquery.integration.common.LoadingUtil.importSecondaryIds) ConqueryConstants(com.bakdata.conquery.ConqueryConstants) IntegrationUtils(com.bakdata.conquery.integration.common.IntegrationUtils) TableImportDescriptor(com.bakdata.conquery.models.preproc.TableImportDescriptor) TestConquery(com.bakdata.conquery.util.support.TestConquery) ShardNode(com.bakdata.conquery.commands.ShardNode) FileUtils(org.apache.commons.io.FileUtils) LoadingUtil(com.bakdata.conquery.integration.common.LoadingUtil) File(java.io.File) ImportId(com.bakdata.conquery.models.identifiable.ids.specific.ImportId) StandaloneSupport(com.bakdata.conquery.util.support.StandaloneSupport) Dataset(com.bakdata.conquery.models.datasets.Dataset) ValidatorHelper(com.bakdata.conquery.models.exceptions.ValidatorHelper) OutputDescription(com.bakdata.conquery.models.preproc.outputs.OutputDescription) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) TableInputDescriptor(com.bakdata.conquery.models.preproc.TableInputDescriptor) JsonIntegrationTest(com.bakdata.conquery.integration.json.JsonIntegrationTest) Query(com.bakdata.conquery.apiv1.query.Query) Jackson(com.bakdata.conquery.io.jackson.Jackson) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) Namespace(com.bakdata.conquery.models.worker.Namespace) Query(com.bakdata.conquery.apiv1.query.Query) QueryTest(com.bakdata.conquery.integration.json.QueryTest) Dataset(com.bakdata.conquery.models.datasets.Dataset) ImportId(com.bakdata.conquery.models.identifiable.ids.specific.ImportId) Namespace(com.bakdata.conquery.models.worker.Namespace) ModificationShieldedWorkerStorage(com.bakdata.conquery.io.storage.ModificationShieldedWorkerStorage) OutputDescription(com.bakdata.conquery.models.preproc.outputs.OutputDescription) ShardNode(com.bakdata.conquery.commands.ShardNode) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) Worker(com.bakdata.conquery.models.worker.Worker) TableInputDescriptor(com.bakdata.conquery.models.preproc.TableInputDescriptor) StandaloneSupport(com.bakdata.conquery.util.support.StandaloneSupport) File(java.io.File) TableImportDescriptor(com.bakdata.conquery.models.preproc.TableImportDescriptor) RequiredTable(com.bakdata.conquery.integration.common.RequiredTable)

Example 3 with MetaStorage

use of com.bakdata.conquery.io.storage.MetaStorage in project conquery by bakdata.

the class PermissionRoleHandlingTest method execute.

/**
 * This is a longer test that plays through different scenarios of permission
 * and role adding/deleting. Creating many objects here to avoid side effects.
 */
@Override
public void execute(StandaloneSupport conquery) throws Exception {
    MetaStorage storage = conquery.getMetaStorage();
    Role mandator1 = new Role("company", "company", storage);
    TestUser user1 = new TestUser(storage);
    Dataset dataset = conquery.getDataset();
    try {
        storage.addRole(mandator1);
        storage.addUser(user1);
        user1.addRole(mandator1);
        user1.addPermission(dataset.createPermission(Ability.READ.asSet()));
        mandator1.addPermission(dataset.createPermission(Ability.DOWNLOAD.asSet()));
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isTrue();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isTrue();
        // Delete permission from mandator
        mandator1.removePermission(dataset.createPermission(Ability.DOWNLOAD.asSet()));
        assertThat(mandator1.getPermissions()).isEmpty();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isTrue();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isFalse();
        // Add permission to user
        user1.addPermission(dataset.createPermission(Ability.DOWNLOAD.asSet()));
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isTrue();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isTrue();
        // Delete permission from mandator
        user1.removePermission(dataset.createPermission(Ability.DOWNLOAD.asSet()));
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isTrue();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isFalse();
        // Add permission to mandator, remove mandator from user
        mandator1.addPermission(dataset.createPermission(Ability.DOWNLOAD.asSet()));
        user1.removeRole(mandator1);
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isTrue();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isFalse();
        // Add mandator back to user
        user1.addRole(mandator1);
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isTrue();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isTrue();
        // Delete all permissions from mandator and user
        user1.removePermission(dataset.createPermission(Ability.READ.asSet()));
        mandator1.removePermission(dataset.createPermission(Ability.DOWNLOAD.asSet()));
        assertThat(user1.isPermitted(dataset.createPermission(Ability.READ.asSet()))).isFalse();
        assertThat(user1.isPermitted(dataset.createPermission(Ability.DOWNLOAD.asSet()))).isFalse();
    } finally {
        storage.removeUser(user1.getId());
        storage.removeRole(mandator1.getId());
    }
}
Also used : Role(com.bakdata.conquery.models.auth.entities.Role) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) Dataset(com.bakdata.conquery.models.datasets.Dataset)

Example 4 with MetaStorage

use of com.bakdata.conquery.io.storage.MetaStorage in project conquery by bakdata.

the class RoleHandlingTest method execute.

@Override
public void execute(StandaloneSupport conquery) throws Exception {
    Dataset dataset1 = new Dataset();
    dataset1.setLabel("dataset1");
    MetaStorage storage = conquery.getMetaStorage();
    Role mandator1 = new Role("company", "company", storage);
    Role mandator1Copy = new Role("company", "company", storage);
    Role mandator2 = new Role("company2", "company2", storage);
    User user1 = new User("user", "user", storage);
    try {
        storage.addRole(mandator1);
        storage.addRole(mandator2);
        storage.addUser(user1);
        // // ADDING
        user1.addRole(mandator1);
        assertThat(user1.getRoles()).containsExactlyInAnyOrder(mandator1.getId());
        user1.addRole(mandator1Copy);
        assertThat(user1.getRoles()).containsExactlyInAnyOrder(mandator1.getId());
        user1.addRole(mandator2);
        assertThat(user1.getRoles()).containsExactlyInAnyOrder(mandator1.getId(), mandator2.getId());
        // // REMOVING
        user1.removeRole(mandator2);
        assertThat(user1.getRoles()).containsExactlyInAnyOrder(mandator1.getId());
        user1.removeRole(mandator1);
        assertThat(user1.getRoles()).isEmpty();
    } finally {
        storage.removeUser(user1.getId());
        storage.removeRole(mandator1.getId());
        storage.removeRole(mandator2.getId());
    }
}
Also used : Role(com.bakdata.conquery.models.auth.entities.Role) User(com.bakdata.conquery.models.auth.entities.User) Dataset(com.bakdata.conquery.models.datasets.Dataset) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage)

Example 5 with MetaStorage

use of com.bakdata.conquery.io.storage.MetaStorage in project conquery by bakdata.

the class RoleUITest method execute.

@Override
public void execute(StandaloneSupport conquery) throws Exception {
    MetaStorage storage = conquery.getMetaStorage();
    Role mandator = new Role("testMandatorName", "testMandatorLabel", storage);
    RoleId mandatorId = mandator.getId();
    User user = new User("testUser@test.de", "testUserName", storage);
    UserId userId = user.getId();
    try {
        ConqueryPermission permission = DatasetPermission.onInstance(Ability.READ.asSet(), new DatasetId("testDatasetId"));
        storage.addRole(mandator);
        storage.addUser(user);
        // override permission object, because it might have changed by the subject
        // owning the permission
        mandator.addPermission(permission);
        user.addRole(mandator);
        URI classBase = HierarchyHelper.hierarchicalPath(conquery.defaultAdminURIBuilder(), RoleUIResource.class, "getRole").buildFromMap(Map.of(ROLE_ID, mandatorId.toString()));
        Response response = conquery.getClient().target(classBase).request().get();
        assertThat(response.getStatus()).isEqualTo(200);
        // Check for Freemarker Errors
        assertThat(response.readEntity(String.class).toLowerCase()).doesNotContain(List.of("freemarker", "debug"));
    } finally {
        storage.removeRole(mandatorId);
        storage.removeUser(userId);
    }
}
Also used : Role(com.bakdata.conquery.models.auth.entities.Role) Response(javax.ws.rs.core.Response) ConqueryPermission(com.bakdata.conquery.models.auth.permissions.ConqueryPermission) User(com.bakdata.conquery.models.auth.entities.User) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) UserId(com.bakdata.conquery.models.identifiable.ids.specific.UserId) RoleUIResource(com.bakdata.conquery.resources.admin.ui.RoleUIResource) RoleId(com.bakdata.conquery.models.identifiable.ids.specific.RoleId) URI(java.net.URI) DatasetId(com.bakdata.conquery.models.identifiable.ids.specific.DatasetId)

Aggregations

MetaStorage (com.bakdata.conquery.io.storage.MetaStorage)23 Dataset (com.bakdata.conquery.models.datasets.Dataset)12 User (com.bakdata.conquery.models.auth.entities.User)9 Role (com.bakdata.conquery.models.auth.entities.Role)8 QueryTest (com.bakdata.conquery.integration.json.QueryTest)7 StandaloneSupport (com.bakdata.conquery.util.support.StandaloneSupport)6 Response (javax.ws.rs.core.Response)6 Query (com.bakdata.conquery.apiv1.query.Query)5 Group (com.bakdata.conquery.models.auth.entities.Group)5 ExecutionState (com.bakdata.conquery.models.execution.ExecutionState)5 DatasetId (com.bakdata.conquery.models.identifiable.ids.specific.DatasetId)5 Namespace (com.bakdata.conquery.models.worker.Namespace)5 ShardNode (com.bakdata.conquery.commands.ShardNode)4 IntegrationUtils (com.bakdata.conquery.integration.common.IntegrationUtils)4 LoadingUtil (com.bakdata.conquery.integration.common.LoadingUtil)4 LoadingUtil.importSecondaryIds (com.bakdata.conquery.integration.common.LoadingUtil.importSecondaryIds)4 JsonIntegrationTest (com.bakdata.conquery.integration.json.JsonIntegrationTest)4 ModificationShieldedWorkerStorage (com.bakdata.conquery.io.storage.ModificationShieldedWorkerStorage)4 ValidatorHelper (com.bakdata.conquery.models.exceptions.ValidatorHelper)4 DatasetRegistry (com.bakdata.conquery.models.worker.DatasetRegistry)4