Search in sources :

Example 11 with RecipeImpl

use of org.eclipse.che.api.machine.server.recipe.RecipeImpl in project che by eclipse.

the class RecipeDaoTest method shouldFindRecipeByType.

@Test(dependsOnMethods = "shouldFindRecipeByUser")
public void shouldFindRecipeByType() throws Exception {
    final RecipeImpl recipe = recipes.get(0);
    final List<RecipeImpl> result = recipeDao.search(null, null, recipe.getType(), 0, recipes.size());
    assertTrue(result.contains(recipe));
}
Also used : RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Test(org.testng.annotations.Test)

Example 12 with RecipeImpl

use of org.eclipse.che.api.machine.server.recipe.RecipeImpl in project che by eclipse.

the class RecipeDownloader method getRecipe.

/**
     * Downloads recipe by location from {@link MachineSource#getLocation()}.
     *
     * @param machineConfig
     *         config used to get recipe location
     * @return recipe with set content and type
     * @throws MachineException
     *         if any error occurs
     */
public RecipeImpl getRecipe(MachineConfig machineConfig) throws MachineException {
    URL recipeUrl;
    File file = null;
    final String location = machineConfig.getSource().getLocation();
    try {
        UriBuilder targetUriBuilder = UriBuilder.fromUri(location);
        // add user token to be able to download user's private recipe
        final URI recipeUri = targetUriBuilder.build();
        if (!recipeUri.isAbsolute() && recipeUri.getHost() == null) {
            targetUriBuilder.scheme(apiEndpoint.getScheme()).host(apiEndpoint.getHost()).port(apiEndpoint.getPort()).replacePath(apiEndpoint.getPath() + location);
            if (EnvironmentContext.getCurrent().getSubject().getToken() != null) {
                targetUriBuilder.queryParam("token", EnvironmentContext.getCurrent().getSubject().getToken());
            }
        }
        recipeUrl = targetUriBuilder.build().toURL();
        file = IoUtil.downloadFileWithRedirect(null, "recipe", null, recipeUrl);
        return new RecipeImpl().withType(machineConfig.getSource().getType()).withScript(IoUtil.readAndCloseQuietly(new FileInputStream(file)));
    } catch (IOException | IllegalArgumentException e) {
        throw new MachineException(format("Failed to download recipe for machine %s. Recipe url %s. Error: %s", machineConfig.getName(), location, e.getLocalizedMessage()));
    } finally {
        if (file != null && !file.delete()) {
            LOG.error(String.format("Removal of recipe file %s failed.", file.getAbsolutePath()));
        }
    }
}
Also used : RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) File(java.io.File) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Example 13 with RecipeImpl

use of org.eclipse.che.api.machine.server.recipe.RecipeImpl in project che by eclipse.

the class JpaRecipeDao method search.

@Override
@Transactional
public List<RecipeImpl> search(String user, List<String> tags, String type, int skipCount, int maxItems) throws ServerException {
    try {
        final EntityManager manager = managerProvider.get();
        final CriteriaBuilder cb = manager.getCriteriaBuilder();
        final CriteriaQuery<RecipeImpl> query = cb.createQuery(RecipeImpl.class);
        final Root<RecipeImpl> fromRecipe = query.from(RecipeImpl.class);
        final ParameterExpression<String> typeParam = cb.parameter(String.class, "recipeType");
        final Predicate checkType = cb.or(cb.isNull(typeParam), cb.equal(fromRecipe.get("type"), typeParam));
        final TypedQuery<RecipeImpl> typedQuery;
        if (tags != null && !tags.isEmpty()) {
            final Join<RecipeImpl, String> tag = fromRecipe.join("tags");
            query.select(cb.construct(RecipeImpl.class, tag.getParent())).where(cb.and(checkType, tag.in(tags))).groupBy(fromRecipe.get("id")).having(cb.equal(cb.count(tag), tags.size()));
            typedQuery = manager.createQuery(query).setParameter("tags", tags);
        } else {
            typedQuery = manager.createQuery(query.where(checkType));
        }
        return typedQuery.setParameter("recipeType", type).setFirstResult(skipCount).setMaxResults(maxItems).getResultList();
    } catch (RuntimeException ex) {
        throw new ServerException(ex.getLocalizedMessage(), ex);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ServerException(org.eclipse.che.api.core.ServerException) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Predicate(javax.persistence.criteria.Predicate) Transactional(com.google.inject.persist.Transactional)

Example 14 with RecipeImpl

use of org.eclipse.che.api.machine.server.recipe.RecipeImpl in project che by eclipse.

the class RecipeDaoTest method shouldFindRecipeByUserTagsAndType.

@Test(dependsOnMethods = { "shouldFindRecipeByUser", "shouldFindingRecipesByTags", "shouldFindRecipeByType" })
public void shouldFindRecipeByUserTagsAndType() throws Exception {
    final RecipeImpl recipe = recipes.get(0);
    final List<RecipeImpl> result = recipeDao.search(null, recipe.getTags(), recipe.getType(), 0, 1);
    assertTrue(result.contains(recipe));
}
Also used : RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Test(org.testng.annotations.Test)

Example 15 with RecipeImpl

use of org.eclipse.che.api.machine.server.recipe.RecipeImpl in project che by eclipse.

the class RecipeDaoTest method shouldUpdateRecipeWithAllRelatedAttributes.

@Test
public void shouldUpdateRecipeWithAllRelatedAttributes() throws Exception {
    final RecipeImpl update = recipes.get(0);
    update.withName("debian").withCreator("userid_9").withDescription("description").withType("docker").setScript("FROM codenvy/debian_jdk8");
    recipeDao.update(update);
    assertEquals(recipeDao.getById(update.getId()), update);
}
Also used : RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Test(org.testng.annotations.Test)

Aggregations

RecipeImpl (org.eclipse.che.api.machine.server.recipe.RecipeImpl)18 Test (org.testng.annotations.Test)8 Transactional (com.google.inject.persist.Transactional)4 EntityManager (javax.persistence.EntityManager)4 ServerException (org.eclipse.che.api.core.ServerException)3 TypeLiteral (com.google.inject.TypeLiteral)2 AccountImpl (org.eclipse.che.account.spi.AccountImpl)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)2 Workspace (org.eclipse.che.api.core.model.workspace.Workspace)2 BeforeRecipeRemovedEvent (org.eclipse.che.api.machine.server.event.BeforeRecipeRemovedEvent)2 SnapshotImpl (org.eclipse.che.api.machine.server.model.impl.SnapshotImpl)2 RecipeDao (org.eclipse.che.api.machine.server.spi.RecipeDao)2 SnapshotDao (org.eclipse.che.api.machine.server.spi.SnapshotDao)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 JsonObject (com.google.gson.JsonObject)1 JpaPersistModule (com.google.inject.persist.jpa.JpaPersistModule)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 URI (java.net.URI)1