Search in sources :

Example 31 with UserDevfileImpl

use of org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl in project devspaces-images by redhat-developer.

the class UserDevfileDaoTest method shouldThrowConflictExceptionWhenCreatingUserDevfileWithExistingId.

@Test(expectedExceptions = ConflictException.class)
public void shouldThrowConflictExceptionWhenCreatingUserDevfileWithExistingId() throws Exception {
    // given
    final UserDevfileImpl devfile = createUserDevfile(accounts[0]);
    final UserDevfileImpl existing = devfiles[0];
    devfile.setId(existing.getId());
    // when
    userDevfileDaoDao.create(devfile);
// then
}
Also used : UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Test(org.testng.annotations.Test)

Example 32 with UserDevfileImpl

use of org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl in project devspaces-images by redhat-developer.

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 33 with UserDevfileImpl

use of org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl in project devspaces-images by redhat-developer.

the class UserDevfileDaoTest method shouldCreateUserDevfile.

@Test(dependsOnMethods = "shouldGetUserDevfileById")
public void shouldCreateUserDevfile() throws Exception {
    // given
    final UserDevfileImpl devfile = createUserDevfile(accounts[0]);
    // when
    userDevfileDaoDao.create(devfile);
    assertEquals(userDevfileDaoDao.getById(devfile.getId()), Optional.of(new UserDevfileImpl(devfile)));
}
Also used : UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Test(org.testng.annotations.Test)

Example 34 with UserDevfileImpl

use of org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl in project devspaces-images by redhat-developer.

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)

Example 35 with UserDevfileImpl

use of org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl in project devspaces-images by redhat-developer.

the class JpaUserDevfileDao method create.

@Override
public UserDevfile create(UserDevfile userDevfile) throws ConflictException, ServerException {
    requireNonNull(userDevfile);
    try {
        Account account = accountDao.getByName(userDevfile.getNamespace());
        UserDevfileImpl userDevfileImpl = new UserDevfileImpl(userDevfile, account);
        doCreate(userDevfileImpl);
        return userDevfileImpl;
    } catch (DuplicateKeyException ex) {
        throw new ConflictException(format("Devfile with name '%s' already exists in the specified account '%s'", userDevfile.getName(), userDevfile.getNamespace()));
    } catch (IntegrityConstraintViolationException ex) {
        throw new ConflictException("Could not create devfile with creator that refers to a non-existent user");
    } catch (RuntimeException ex) {
        throw new ServerException(ex.getMessage(), ex);
    } catch (NotFoundException e) {
        throw new ConflictException(format("Not able to create devfile in requested namespace %s bacause it is not found", userDevfile.getNamespace()));
    }
}
Also used : Account(org.eclipse.che.account.shared.model.Account) IntegrityConstraintViolationException(org.eclipse.che.core.db.jpa.IntegrityConstraintViolationException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) NotFoundException(org.eclipse.che.api.core.NotFoundException) DuplicateKeyException(org.eclipse.che.core.db.jpa.DuplicateKeyException)

Aggregations

UserDevfileImpl (org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl)74 Test (org.testng.annotations.Test)46 UserDevfile (org.eclipse.che.api.core.model.workspace.devfile.UserDevfile)28 TestObjectGenerator.createUserDevfile (org.eclipse.che.api.devfile.server.TestObjectGenerator.createUserDevfile)18 AccountImpl (org.eclipse.che.account.spi.AccountImpl)16 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)16 TypeLiteral (com.google.inject.TypeLiteral)14 TckResourcesCleaner (org.eclipse.che.commons.test.tck.TckResourcesCleaner)14 DBInitializer (org.eclipse.che.core.db.DBInitializer)14 SchemaInitializer (org.eclipse.che.core.db.schema.SchemaInitializer)14 FlywaySchemaInitializer (org.eclipse.che.core.db.schema.impl.flyway.FlywaySchemaInitializer)14 AccountDao (org.eclipse.che.account.spi.AccountDao)12 Response (io.restassured.response.Response)10 JpaAccountDao (org.eclipse.che.account.spi.jpa.JpaAccountDao)10 Pair (org.eclipse.che.commons.lang.Pair)10 PersistTestModuleBuilder (org.eclipse.che.commons.test.db.PersistTestModuleBuilder)10 Transactional (com.google.inject.persist.Transactional)8 EntityManager (javax.persistence.EntityManager)8 UserDevfileDto (org.eclipse.che.api.devfile.shared.dto.UserDevfileDto)8 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)8