use of org.eclipse.che.api.factory.shared.dto.FactoryDto in project che by eclipse.
the class FactoryBuilder method build.
/**
* Build factory from json and validate its compatibility.
*
* @param json
* - json InputStream from encoded factory.
* @return - Factory object represented by given factory json.
*/
public FactoryDto build(InputStream json) throws IOException, ConflictException {
FactoryDto factory = DtoFactory.getInstance().createDtoFromJson(json, FactoryDto.class);
checkValid(factory);
return factory;
}
use of org.eclipse.che.api.factory.shared.dto.FactoryDto in project che by eclipse.
the class FactoryService method saveFactory.
@POST
@Consumes(MULTIPART_FORM_DATA)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Create a new factory based on configuration and factory images", notes = "The field 'factory' is required")
@ApiResponses({ @ApiResponse(code = 200, message = "Factory successfully created"), @ApiResponse(code = 400, message = "Missed required parameters, parameters are not valid"), @ApiResponse(code = 403, message = "The user does not have rights to create factory"), @ApiResponse(code = 409, message = "When factory with given name and creator already exists"), @ApiResponse(code = 500, message = "Internal server error occurred") })
public FactoryDto saveFactory(Iterator<FileItem> formData) throws ForbiddenException, ConflictException, BadRequestException, ServerException {
try {
final Set<FactoryImage> images = new HashSet<>();
FactoryDto factory = null;
while (formData.hasNext()) {
final FileItem item = formData.next();
switch(item.getFieldName()) {
case ("factory"):
{
try (InputStream factoryData = item.getInputStream()) {
factory = factoryBuilder.build(factoryData);
} catch (JsonSyntaxException ex) {
throw new BadRequestException("Invalid JSON value of the field 'factory' provided");
}
break;
}
case ("image"):
{
try (InputStream imageData = item.getInputStream()) {
final FactoryImage image = createImage(imageData, item.getContentType(), NameGenerator.generate(null, 16));
if (image.hasContent()) {
images.add(image);
}
}
break;
}
default:
}
}
requiredNotNull(factory, "factory configuration");
processDefaults(factory);
createValidator.validateOnCreate(factory);
return injectLinks(asDto(factoryManager.saveFactory(factory, images)), images);
} catch (IOException ioEx) {
throw new ServerException(ioEx.getLocalizedMessage(), ioEx);
}
}
use of org.eclipse.che.api.factory.shared.dto.FactoryDto in project che by eclipse.
the class FactoryServiceTest method shouldReturnFactoryListByNameAttribute.
@Test
public void shouldReturnFactoryListByNameAttribute() throws Exception {
final Factory factory = createFactory();
when(factoryManager.getByAttribute(1, 0, ImmutableList.of(Pair.of("factory.name", factory.getName())))).thenReturn(ImmutableList.of(factory));
final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType(APPLICATION_JSON).when().expect().statusCode(200).get(SERVICE_PATH + "/find?maxItems=1&skipCount=0&factory.name=" + factory.getName());
final List<FactoryDto> res = unwrapDtoList(response, FactoryDto.class);
assertEquals(res.size(), 1);
assertEquals(res.get(0).withLinks(emptyList()), asDto(factory, user));
}
use of org.eclipse.che.api.factory.shared.dto.FactoryDto in project che by eclipse.
the class FactoryServiceTest method shouldReturnFactoryByIdentifierWithValidation.
@Test
public void shouldReturnFactoryByIdentifierWithValidation() throws Exception {
final Factory factory = createFactory();
final FactoryDto factoryDto = asDto(factory, user);
when(factoryManager.getById(FACTORY_ID)).thenReturn(factory);
when(factoryManager.getFactoryImages(FACTORY_ID)).thenReturn(emptySet());
doNothing().when(acceptValidator).validateOnAccept(any(FactoryDto.class));
final Response response = given().when().expect().statusCode(200).get(SERVICE_PATH + "/" + FACTORY_ID + "?validate=true");
assertEquals(getFromResponse(response, FactoryDto.class).withLinks(emptyList()), factoryDto);
}
use of org.eclipse.che.api.factory.shared.dto.FactoryDto in project che by eclipse.
the class FactoryServiceTest method shouldSaveFactoryWithoutImages.
@Test
public void shouldSaveFactoryWithoutImages() throws Exception {
final Factory factory = createFactory();
final FactoryDto factoryDto = asDto(factory, user);
when(factoryManager.saveFactory(any(FactoryDto.class))).thenReturn(factory);
final Response response = given().auth().basic(ADMIN_USER_NAME, ADMIN_USER_PASSWORD).contentType(ContentType.JSON).body(factoryDto).expect().statusCode(200).post(SERVICE_PATH);
assertEquals(getFromResponse(response, FactoryDto.class).withLinks(emptyList()), factoryDto);
}
Aggregations