use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.
the class MultiuserJpaUserDevfileDaoTest method shouldFindDevfilesByByPermissions.
@Test
public void shouldFindDevfilesByByPermissions() throws Exception {
EnvironmentContext expected = EnvironmentContext.getCurrent();
expected.setSubject(new SubjectImpl("user", users.get(0).getId(), "token", false));
List<UserDevfile> results = dao.getDevfiles(30, 0, Collections.emptyList(), Collections.emptyList()).getItems();
assertEquals(results.size(), 2);
assertTrue(results.contains(userDevfiles.get(0)));
assertTrue(results.contains(userDevfiles.get(1)));
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.
the class DevfileService method getUserDevfiles.
@GET
@Path("search")
@Produces(APPLICATION_JSON)
@Operation(summary = "Get devfiles which user can read. This operation can be performed only by authorized user. " + "It is possible to add additional constraints for the desired devfiles by specifying\n" + "multiple query parameters that is representing fields of the devfile. All constrains\n" + "would be combined with \"And\" condition. Also, it is possible to specify 'like:' prefix\n" + "for the query parameters. In this case instead of an exact match would be used SQL pattern like search.\n" + "Examples id=sdfsdf5&devfile.meta.name=like:%dfdf&", responses = { @ApiResponse(responseCode = "200", description = "The devfiles successfully fetched", content = @Content(array = @ArraySchema(schema = @Schema(implementation = UserDevfileDto.class)))), @ApiResponse(responseCode = "500", description = "Internal server error occurred during devfiles fetching") })
public Response getUserDevfiles(@Parameter(description = "The number of the items to skip") @DefaultValue("0") @QueryParam("skipCount") Integer skipCount, @Parameter(description = "The limit of the items in the response, default is 30, maximum 60") @DefaultValue("30") @QueryParam("maxItems") Integer maxItems, @Parameter(description = "A list of fields and directions of sort. By default items would be sorted by id. Example id:asc,name:desc.") @QueryParam("order") String order) throws ServerException, BadRequestException {
final Set<String> skip = ImmutableSet.of("token", "skipCount", "maxItems", "order");
Map<String, Set<String>> queryParams = URLEncodedUtils.parse(uriInfo.getRequestUri());
final List<Pair<String, String>> query = queryParams.entrySet().stream().filter(param -> !param.getValue().isEmpty()).filter(param -> !skip.contains(param.getKey())).map(entry -> Pair.of(entry.getKey(), entry.getValue().iterator().next())).collect(toList());
List<Pair<String, String>> searchOrder = Collections.emptyList();
if (order != null && !order.isEmpty()) {
try {
searchOrder = Splitter.on(",").trimResults().omitEmptyStrings().withKeyValueSeparator(":").split(order).entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue())).collect(toList());
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid `order` query parameter format." + e.getMessage());
}
}
Page<? extends UserDevfile> userDevfilesPage = userDevfileManager.getUserDevfiles(maxItems, skipCount, query, searchOrder);
List<UserDevfileDto> list = userDevfilesPage.getItems().stream().map(DtoConverter::asDto).map(dto -> linksInjector.injectLinks(asDto(dto), getServiceContext())).collect(toList());
return Response.ok().entity(list).header("Link", createLinkHeader(userDevfilesPage)).build();
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.
the class UserDevfileManager method createDevfile.
/**
* Stores {@link Devfile} instance
*
* @param userDevfile instance of user devfile which would be stored
* @return new persisted devfile instance
* @throws ConflictException when any conflict occurs (e.g Devfile with such name already exists
* for {@code owner})
* @throws NullPointerException when {@code devfile} is null
* @throws ServerException when any other error occurs
*/
public UserDevfile createDevfile(UserDevfile userDevfile) throws ServerException, NotFoundException, ConflictException {
requireNonNull(userDevfile, "Required non-null userdevfile");
requireNonNull(userDevfile.getDevfile(), "Required non-null devfile");
String name = userDevfile.getName() != null ? userDevfile.getName() : NameGenerator.generate("devfile-", 5);
UserDevfile result = userDevfileDao.create(new UserDevfileImpl(NameGenerator.generate("id-", 16), accountManager.getByName(EnvironmentContext.getCurrent().getSubject().getUserName()), name, userDevfile.getDescription(), userDevfile.getDevfile()));
LOG.debug("UserDevfile '{}' with id '{}' created by user '{}'", result.getName(), result.getId(), EnvironmentContext.getCurrent().getSubject().getUserName());
eventService.publish(new DevfileCreatedEvent(result));
return result;
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.
the class UserDevfileManager method updateUserDevfile.
/**
* Updates an existing user devfile in accordance to the new configuration.
*
* <p>Note: Replace strategy is used for user devfile update, it means that existing devfile data
* will be replaced with given {@code update}.
*
* @param update user devfile update
* @return updated user devfile
* @throws NullPointerException when {@code update} is null
* @throws ConflictException when any conflict occurs.
* @throws NotFoundException when user devfile with given id not found
* @throws ServerException when any server error occurs
*/
public UserDevfile updateUserDevfile(UserDevfile update) throws ConflictException, NotFoundException, ServerException {
requireNonNull(update);
Optional<UserDevfile> result = userDevfileDao.update(update);
UserDevfile devfile = result.orElseThrow(() -> new NotFoundException(format("Devfile with id '%s' doesn't exist", update.getId())));
LOG.debug("UserDevfile '{}' with id '{}' update by user '{}'", devfile.getName(), devfile.getId(), EnvironmentContext.getCurrent().getSubject().getUserName());
eventService.publish(new DevfileUpdatedEvent(devfile));
return devfile;
}
use of org.eclipse.che.api.core.model.workspace.devfile.UserDevfile in project che-server by eclipse-che.
the class DevfileServiceTest method shouldThrowNotFoundExceptionWhenUpdatingNonExistingUserDevfile.
@Test
public void shouldThrowNotFoundExceptionWhenUpdatingNonExistingUserDevfile() throws Exception {
// given
final UserDevfile userDevfile = DtoConverter.asDto(TestObjectGenerator.createUserDevfile("devfile-name"));
doThrow(new NotFoundException(format("User devfile with id %s is not found.", USER_DEVFILE_ID))).when(userDevfileManager).updateUserDevfile(any(UserDevfile.class));
// when
final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType(APPLICATION_JSON).body(DtoFactory.getInstance().toJson(userDevfile)).when().put(SECURE_PATH + "/devfile/" + USER_DEVFILE_ID);
// then
assertEquals(response.getStatusCode(), 404);
assertEquals(unwrapDto(response, ServiceError.class).getMessage(), format("User devfile with id %s is not found.", USER_DEVFILE_ID));
}
Aggregations