Search in sources :

Example 16 with FactoryDto

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;
}
Also used : FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto)

Example 17 with FactoryDto

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);
    }
}
Also used : FileItem(org.apache.commons.fileupload.FileItem) JsonSyntaxException(com.google.gson.JsonSyntaxException) ServerException(org.eclipse.che.api.core.ServerException) InputStream(java.io.InputStream) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) BadRequestException(org.eclipse.che.api.core.BadRequestException) IOException(java.io.IOException) HashSet(java.util.HashSet) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 18 with FactoryDto

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));
}
Also used : Response(com.jayway.restassured.response.Response) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) DtoFactory(org.eclipse.che.dto.server.DtoFactory) Factory(org.eclipse.che.api.core.model.factory.Factory) Test(org.testng.annotations.Test)

Example 19 with FactoryDto

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);
}
Also used : Response(com.jayway.restassured.response.Response) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) DtoFactory(org.eclipse.che.dto.server.DtoFactory) Factory(org.eclipse.che.api.core.model.factory.Factory) Test(org.testng.annotations.Test)

Example 20 with 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);
}
Also used : Response(com.jayway.restassured.response.Response) FactoryDto(org.eclipse.che.api.factory.shared.dto.FactoryDto) DtoFactory(org.eclipse.che.dto.server.DtoFactory) Factory(org.eclipse.che.api.core.model.factory.Factory) Test(org.testng.annotations.Test)

Aggregations

FactoryDto (org.eclipse.che.api.factory.shared.dto.FactoryDto)30 Test (org.testng.annotations.Test)21 Response (com.jayway.restassured.response.Response)11 DtoFactory (org.eclipse.che.dto.server.DtoFactory)11 Factory (org.eclipse.che.api.core.model.factory.Factory)10 IdeActionDto (org.eclipse.che.api.factory.shared.dto.IdeActionDto)8 IdeDto (org.eclipse.che.api.factory.shared.dto.IdeDto)8 InputStream (java.io.InputStream)6 HashMap (java.util.HashMap)5 BadRequestException (org.eclipse.che.api.core.BadRequestException)4 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 IOException (java.io.IOException)3 HashSet (java.util.HashSet)3 Produces (javax.ws.rs.Produces)3 Matchers.anyString (org.mockito.Matchers.anyString)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 List (java.util.List)2