Search in sources :

Example 16 with UserDevfileImpl

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

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

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

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)

Example 18 with UserDevfileImpl

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

the class JpaUserDevfileDao method doUpdate.

@Transactional
protected Optional<UserDevfileImpl> doUpdate(UserDevfileImpl update) {
    final EntityManager manager = managerProvider.get();
    if (manager.find(UserDevfileImpl.class, update.getId()) == null) {
        return Optional.empty();
    }
    UserDevfileImpl merged = manager.merge(update);
    manager.flush();
    return Optional.of(merged);
}
Also used : EntityManager(javax.persistence.EntityManager) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) Transactional(com.google.inject.persist.Transactional)

Example 19 with UserDevfileImpl

use of org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl 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)

Example 20 with UserDevfileImpl

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

the class DevfileServiceTest method shouldCreateUserDevfileFromJson.

@Test(dataProvider = "validUserDevfiles")
public void shouldCreateUserDevfileFromJson(UserDevfileDto userDevfileDto) throws Exception {
    final UserDevfileImpl userDevfileImpl = new UserDevfileImpl("id-123123", TEST_ACCOUNT, userDevfileDto);
    when(userDevfileManager.createDevfile(any(UserDevfile.class))).thenReturn(userDevfileImpl);
    final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType("application/json").body(DtoFactory.getInstance().toJson(userDevfileDto)).when().post(SECURE_PATH + "/devfile");
    assertEquals(response.getStatusCode(), 201);
    UserDevfileDto dto = unwrapDto(response, UserDevfileDto.class);
    assertEquals(dto.getNamespace(), TEST_ACCOUNT.getName());
    assertEquals(new UserDevfileImpl(dto, TEST_ACCOUNT), userDevfileImpl);
    verify(userDevfileManager).createDevfile(any(UserDevfile.class));
}
Also used : Response(io.restassured.response.Response) UserDevfile(org.eclipse.che.api.core.model.workspace.devfile.UserDevfile) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) UserDevfileDto(org.eclipse.che.api.devfile.shared.dto.UserDevfileDto) Test(org.testng.annotations.Test)

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