Search in sources :

Example 11 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class EntityDataStoreIT method audit.

@Test
public void audit() throws JsonProcessingException {
    // Entity
    Branch branch = do_create_branch();
    // Adds some data
    String name = uid("T");
    EntityDataStoreRecord record = store.add(branch, CATEGORY, name, Signature.of(TEST_USER), null, new IntNode(15));
    // Gets the audit
    List<EntityDataStoreRecordAudit> audit = store.getRecordAudit(record.getId());
    assertEquals(1, audit.size());
    assertEquals(EntityDataStoreRecordAuditType.CREATED, audit.get(0).getType());
    assertEquals(TEST_USER, audit.get(0).getSignature().getUser().getName());
    // Updates the same name
    store.replaceOrAdd(branch, CATEGORY, name, Signature.of("other"), null, new IntNode(16));
    // Checks
    audit = store.getRecordAudit(record.getId());
    assertEquals(2, audit.size());
    assertEquals(EntityDataStoreRecordAuditType.UPDATED, audit.get(0).getType());
    assertEquals("other", audit.get(0).getSignature().getUser().getName());
    assertEquals(EntityDataStoreRecordAuditType.CREATED, audit.get(1).getType());
    assertEquals(TEST_USER, audit.get(1).getSignature().getUser().getName());
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) Branch(net.nemerosa.ontrack.model.structure.Branch) Test(org.junit.Test)

Example 12 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class EntityDataStoreIT method getByFilter.

@Test
public void getByFilter() {
    // Entities
    Branch branch1 = do_create_branch();
    Branch branch2 = do_create_branch();
    // Adds some data
    store.deleteAll();
    store.addObject(branch1, "C1", "N1", Signature.of(TEST_USER), null, 1);
    store.addObject(branch1, "C1", "N1", Signature.of(TEST_USER), null, 2);
    store.addObject(branch1, "C1", "N1", Signature.of(TEST_USER), null, 3);
    store.addObject(branch1, "C1", "N1", Signature.of(TEST_USER), null, 4);
    store.addObject(branch1, "C1", "N2", Signature.of(TEST_USER), null, 5);
    store.addObject(branch1, "C2", "N3", Signature.of(TEST_USER), null, 6);
    store.addObject(branch2, "C1", "N1", Signature.of(TEST_USER), null, 7);
    store.addObject(branch2, "C1", "N2", Signature.of(TEST_USER), null, 8);
    store.addObject(branch2, "C2", "N3", Signature.of(TEST_USER), null, 9);
    // Checks
    assertEquals(6, store.getByFilter(new EntityDataStoreFilter(branch1)).size());
    assertEquals(5, store.getByFilter(new EntityDataStoreFilter(branch1).withCategory("C1")).size());
    assertEquals(4, store.getByFilter(new EntityDataStoreFilter(branch1).withCategory("C1").withName("N1")).size());
}
Also used : Branch(net.nemerosa.ontrack.model.structure.Branch) Test(org.junit.Test)

Example 13 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class BuildFilterServiceImpl method getBuildFilters.

@Override
public Collection<BuildFilterResource<?>> getBuildFilters(ID branchId) {
    Branch branch = structureService.getBranch(branchId);
    // Are we logged?
    Account account = securityService.getCurrentAccount();
    if (account != null) {
        // Gets the filters for this account and the branch
        return buildFilterRepository.findForBranch(OptionalInt.of(account.id()), branchId.getValue()).stream().map(t -> loadBuildFilterResource(branch, t)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    } else // Not logged, no filter
    {
        // Gets the filters for the branch
        return buildFilterRepository.findForBranch(OptionalInt.empty(), branchId.get()).stream().map(t -> loadBuildFilterResource(branch, t)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    }
}
Also used : Ack(net.nemerosa.ontrack.model.Ack) BuildFilterNotFoundException(net.nemerosa.ontrack.model.exceptions.BuildFilterNotFoundException) BranchFilterMgt(net.nemerosa.ontrack.model.security.BranchFilterMgt) StandardBuildFilterData(net.nemerosa.ontrack.model.structure.StandardBuildFilterData) StructureService(net.nemerosa.ontrack.model.structure.StructureService) BuildFilterRepository(net.nemerosa.ontrack.repository.BuildFilterRepository) Collection(java.util.Collection) Account(net.nemerosa.ontrack.model.security.Account) Autowired(org.springframework.beans.factory.annotation.Autowired) TBuildFilter(net.nemerosa.ontrack.repository.TBuildFilter) BuildFilterNotLoggedException(net.nemerosa.ontrack.model.exceptions.BuildFilterNotLoggedException) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) SecurityService(net.nemerosa.ontrack.model.security.SecurityService) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) Map(java.util.Map) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) net.nemerosa.ontrack.model.buildfilter(net.nemerosa.ontrack.model.buildfilter) ID(net.nemerosa.ontrack.model.structure.ID) Branch(net.nemerosa.ontrack.model.structure.Branch) Transactional(org.springframework.transaction.annotation.Transactional) Account(net.nemerosa.ontrack.model.security.Account) Optional(java.util.Optional) Branch(net.nemerosa.ontrack.model.structure.Branch)

Example 14 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class BuildFilterServiceImpl method saveFilter.

@Override
public Ack saveFilter(ID branchId, boolean shared, String name, String type, JsonNode parameters) {
    // Checks the account
    if (shared) {
        Account account = securityService.getCurrentAccount();
        // Gets the branch
        Branch branch = structureService.getBranch(branchId);
        // Checks access rights
        securityService.checkProjectFunction(branch, BranchFilterMgt.class);
        // Deletes any previous filter
        int currentAccountId = account.id();
        buildFilterRepository.findByBranchAndName(currentAccountId, branchId.get(), name).ifPresent((filter) -> buildFilterRepository.delete(currentAccountId, branchId.get(), name, true));
        // No account to be used
        return doSaveFilter(OptionalInt.empty(), branchId, name, type, parameters);
    } else {
        Account account = securityService.getCurrentAccount();
        if (account == null) {
            return Ack.NOK;
        } else {
            // Saves it for this account
            return doSaveFilter(OptionalInt.of(account.id()), branchId, name, type, parameters);
        }
    }
}
Also used : Account(net.nemerosa.ontrack.model.security.Account) Branch(net.nemerosa.ontrack.model.structure.Branch)

Example 15 with Branch

use of net.nemerosa.ontrack.model.structure.Branch in project ontrack by nemerosa.

the class BuildFilterServiceIT method delete_shared_filter.

@Test
public void delete_shared_filter() throws Exception {
    // Branch
    Branch branch = doCreateBranch();
    // Account for the tests
    Account account = doCreateAccount();
    // Creates a shared filter for this account
    Ack ack = asAccount(account).with(branch, BranchFilterMgt.class).call(() -> buildFilterService.saveFilter(branch.getId(), // Shared
    true, "MyFilter", NamedBuildFilterProvider.class.getName(), objectMapper.valueToTree(NamedBuildFilterData.of("1"))));
    assertTrue("Account filter saved", ack.isSuccess());
    // The filter is present for this account
    Collection<BuildFilterResource<?>> filters = asAccount(account).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals(1, filters.size());
    BuildFilterResource<?> filter = filters.iterator().next();
    assertEquals("MyFilter", filter.getName());
    assertTrue(filter.isShared());
    // ... and that it is available also for not logged users
    filters = asUser().withId(10).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals("Account filter available for everybody else", 1, filters.size());
    filter = filters.iterator().next();
    assertEquals("MyFilter", filter.getName());
    assertTrue(filter.isShared());
    // Deletes the filter
    asAccount(account).call(() -> buildFilterService.deleteFilter(branch.getId(), "MyFilter"));
    // The filter is no longer there for the account
    filters = asAccount(account).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals(0, filters.size());
    // ... not for unlogged users
    filters = asUser().withId(10).withView(branch).call(() -> buildFilterService.getBuildFilters(branch.getId()));
    assertEquals("Account filter not available for everybody else", 0, filters.size());
}
Also used : Account(net.nemerosa.ontrack.model.security.Account) Branch(net.nemerosa.ontrack.model.structure.Branch) BuildFilterResource(net.nemerosa.ontrack.model.buildfilter.BuildFilterResource) Ack(net.nemerosa.ontrack.model.Ack) BranchFilterMgt(net.nemerosa.ontrack.model.security.BranchFilterMgt) Test(org.junit.Test)

Aggregations

Branch (net.nemerosa.ontrack.model.structure.Branch)33 Test (org.junit.Test)29 IntNode (com.fasterxml.jackson.databind.node.IntNode)12 Account (net.nemerosa.ontrack.model.security.Account)7 Ack (net.nemerosa.ontrack.model.Ack)6 Project (net.nemerosa.ontrack.model.structure.Project)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 BuildFilterResource (net.nemerosa.ontrack.model.buildfilter.BuildFilterResource)4 NameDescription (net.nemerosa.ontrack.model.structure.NameDescription)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 HttpOutputMessage (org.springframework.http.HttpOutputMessage)3 IOException (java.io.IOException)2 Collectors (java.util.stream.Collectors)2 BranchFilterMgt (net.nemerosa.ontrack.model.security.BranchFilterMgt)2 ProjectView (net.nemerosa.ontrack.model.security.ProjectView)2 ID (net.nemerosa.ontrack.model.structure.ID)2 StructureService (net.nemerosa.ontrack.model.structure.StructureService)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 GraphQLInt (graphql.Scalars.GraphQLInt)1 GraphQLString (graphql.Scalars.GraphQLString)1