Search in sources :

Example 6 with UserDevfile

use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.

the class UserDevfileDaoTest method shouldGetDevfilesByNamespace.

@Test
public void shouldGetDevfilesByNamespace() throws Exception {
    final UserDevfileImpl devfile1 = devfiles[0];
    final UserDevfileImpl devfile2 = devfiles[1];
    assertEquals(devfile1.getNamespace(), devfile2.getNamespace(), "Namespaces must be the same");
    final Page<UserDevfile> found = userDevfileDaoDao.getByNamespace(devfile1.getNamespace(), 6, 0);
    assertEquals(found.getTotalItemsCount(), 2);
    assertEquals(found.getItemsCount(), 2);
    assertEquals(new HashSet<>(found.getItems()), new HashSet<>(asList(devfile1, devfile2)));
}
Also used : UserDevfile(org.eclipse.che.api.core.model.workspace.devfile.UserDevfile) TestObjectGenerator.createUserDevfile(org.eclipse.che.api.devfile.server.TestObjectGenerator.createUserDevfile) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Test(org.testng.annotations.Test)

Example 7 with UserDevfile

use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.

the class UserDevfileDaoTest method shouldBeAbleToGetAvailableToUserDevfilesWithFilter2.

@Test
public void shouldBeAbleToGetAvailableToUserDevfilesWithFilter2() throws ServerException, NotFoundException, ConflictException {
    // given
    final UserDevfileImpl update = devfiles[0];
    update.setName("New345Name");
    userDevfileDaoDao.update(update);
    // when
    final Page<UserDevfile> result = userDevfileDaoDao.getDevfiles(30, 0, ImmutableList.of(new Pair<>("name", "like:%w345N%")), Collections.emptyList());
    // then
    assertEquals(new HashSet<>(result.getItems()), ImmutableSet.of(update));
}
Also used : UserDevfile(org.eclipse.che.api.core.model.workspace.devfile.UserDevfile) TestObjectGenerator.createUserDevfile(org.eclipse.che.api.devfile.server.TestObjectGenerator.createUserDevfile) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Pair(org.eclipse.che.commons.lang.Pair) Test(org.testng.annotations.Test)

Example 8 with UserDevfile

use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.

the class UserDevfileDaoTest method shouldCreateUserDevfileWithNullDescription.

@Test
public void shouldCreateUserDevfileWithNullDescription() throws Exception {
    // given
    final UserDevfileImpl devfile = createUserDevfile(accounts[0]);
    devfile.setDescription(null);
    // when
    userDevfileDaoDao.create(devfile);
    Optional<UserDevfile> devfileOptional = userDevfileDaoDao.getById(devfile.getId());
    assertTrue(devfileOptional.isPresent());
    assertNull(devfileOptional.get().getDescription());
    assertEquals(devfileOptional, Optional.of(new UserDevfileImpl(devfile)));
}
Also used : UserDevfile(org.eclipse.che.api.core.model.workspace.devfile.UserDevfile) TestObjectGenerator.createUserDevfile(org.eclipse.che.api.devfile.server.TestObjectGenerator.createUserDevfile) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Test(org.testng.annotations.Test)

Example 9 with UserDevfile

use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.

the class UserDevfileDaoTest method shouldBeAbleToGetAvailableToUserDevfilesWithFilterAndLimit.

@Test
public void shouldBeAbleToGetAvailableToUserDevfilesWithFilterAndLimit() throws ServerException, NotFoundException, ConflictException {
    // given
    final UserDevfileImpl update = devfiles[0];
    update.setName("New345Name");
    userDevfileDaoDao.update(update);
    // when
    final Page<UserDevfile> result = userDevfileDaoDao.getDevfiles(12, 0, ImmutableList.of(new Pair<>("name", "like:devfileName%")), Collections.emptyList());
    // then
    assertEquals(result.getItems().size(), 9);
}
Also used : UserDevfile(org.eclipse.che.api.core.model.workspace.devfile.UserDevfile) TestObjectGenerator.createUserDevfile(org.eclipse.che.api.devfile.server.TestObjectGenerator.createUserDevfile) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Pair(org.eclipse.che.commons.lang.Pair) Test(org.testng.annotations.Test)

Example 10 with UserDevfile

use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.

the class JpaUserDevfileDao method doGetDevfiles.

@Transactional(rollbackOn = { ServerException.class })
protected Page<UserDevfile> doGetDevfiles(int maxItems, int skipCount, List<Pair<String, String>> filter, List<Pair<String, String>> order, Supplier<UserDevfileSearchQueryBuilder> queryBuilderSupplier) throws ServerException {
    if (filter != null && !filter.isEmpty()) {
        List<Pair<String, String>> invalidFilter = filter.stream().filter(p -> !VALID_SEARCH_FIELDS.contains(p.first.toLowerCase())).collect(toList());
        if (!invalidFilter.isEmpty()) {
            throw new IllegalArgumentException("Filtering allowed only by " + VALID_SEARCH_FIELDS + " but got: " + invalidFilter);
        }
    }
    List<Pair<String, String>> effectiveOrder = DEFAULT_ORDER;
    if (order != null && !order.isEmpty()) {
        List<Pair<String, String>> invalidOrder = order.stream().filter(p -> !VALID_ORDER_FIELDS.contains(p.first.toLowerCase())).collect(toList());
        if (!invalidOrder.isEmpty()) {
            throw new IllegalArgumentException("Order allowed only by " + VALID_ORDER_FIELDS + "¬ but got: " + invalidOrder);
        }
        List<Pair<String, String>> invalidSortOrder = order.stream().filter(p -> !p.second.equalsIgnoreCase("asc") && !p.second.equalsIgnoreCase("desc")).collect(Collectors.toList());
        if (!invalidSortOrder.isEmpty()) {
            throw new IllegalArgumentException("Invalid sort order direction. Possible values are 'asc' or 'desc' but got: " + invalidSortOrder);
        }
        effectiveOrder = order;
    }
    try {
        final long count = queryBuilderSupplier.get().withFilter(filter).buildCountQuery().getSingleResult();
        if (count == 0) {
            return new Page<>(emptyList(), skipCount, maxItems, count);
        }
        List<UserDevfileImpl> result = queryBuilderSupplier.get().withFilter(filter).withOrder(effectiveOrder).withMaxItems(maxItems).withSkipCount(skipCount).buildSelectItemsQuery().getResultList().stream().map(UserDevfileImpl::new).collect(toList());
        return new Page<>(result, skipCount, maxItems, count);
    } catch (RuntimeException x) {
        throw new ServerException(x.getLocalizedMessage(), x);
    }
}
Also used : Provider(javax.inject.Provider) Supplier(com.google.common.base.Supplier) Page(org.eclipse.che.api.core.Page) UserDevfileDao(org.eclipse.che.api.devfile.server.spi.UserDevfileDao) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) TypedQuery(javax.persistence.TypedQuery) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) BeforeDevfileRemovedEvent(org.eclipse.che.api.devfile.server.event.BeforeDevfileRemovedEvent) Transactional(com.google.inject.persist.Transactional) Inject(javax.inject.Inject) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) ConflictException(org.eclipse.che.api.core.ConflictException) Account(org.eclipse.che.account.shared.model.Account) UserDevfile(org.eclipse.che.api.core.model.workspace.devfile.UserDevfile) EventService(org.eclipse.che.api.core.notification.EventService) ImmutableSet(com.google.common.collect.ImmutableSet) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) EntityManager(javax.persistence.EntityManager) Collectors(java.util.stream.Collectors) Pair(org.eclipse.che.commons.lang.Pair) String.format(java.lang.String.format) NotFoundException(org.eclipse.che.api.core.NotFoundException) Beta(com.google.common.annotations.Beta) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AccountDao(org.eclipse.che.account.spi.AccountDao) ServerException(org.eclipse.che.api.core.ServerException) DuplicateKeyException(org.eclipse.che.core.db.jpa.DuplicateKeyException) IntegrityConstraintViolationException(org.eclipse.che.core.db.jpa.IntegrityConstraintViolationException) StringJoiner(java.util.StringJoiner) Optional(java.util.Optional) UserDevfileSearchQueryBuilder.newBuilder(org.eclipse.che.api.devfile.server.jpa.JpaUserDevfileDao.UserDevfileSearchQueryBuilder.newBuilder) ServerException(org.eclipse.che.api.core.ServerException) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Page(org.eclipse.che.api.core.Page) Pair(org.eclipse.che.commons.lang.Pair) Transactional(com.google.inject.persist.Transactional)

Aggregations

UserDevfile (org.eclipse.che.api.core.model.workspace.devfile.UserDevfile)34 Test (org.testng.annotations.Test)24 UserDevfileImpl (org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl)22 TestObjectGenerator.createUserDevfile (org.eclipse.che.api.devfile.server.TestObjectGenerator.createUserDevfile)20 NotFoundException (org.eclipse.che.api.core.NotFoundException)10 Pair (org.eclipse.che.commons.lang.Pair)8 ImmutableSet (com.google.common.collect.ImmutableSet)4 List (java.util.List)4 Map (java.util.Map)4 Set (java.util.Set)4 Collectors.toList (java.util.stream.Collectors.toList)4 Inject (javax.inject.Inject)4 ConflictException (org.eclipse.che.api.core.ConflictException)3 Page (org.eclipse.che.api.core.Page)3 ServerException (org.eclipse.che.api.core.ServerException)3 Beta (com.google.common.annotations.Beta)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Splitter (com.google.common.base.Splitter)2 Supplier (com.google.common.base.Supplier)2 ImmutableList (com.google.common.collect.ImmutableList)2