Search in sources :

Example 1 with UploadModelResult

use of org.eclipse.vorto.repository.importer.UploadModelResult in project vorto by eclipse.

the class ImportController method uploadModel.

@RequestMapping(method = RequestMethod.POST)
@PreAuthorize("hasAuthority('model_creator')")
public ResponseEntity<UploadModelResult> uploadModel(@ApiParam(value = "The vorto model file to upload", required = true) @RequestParam("file") MultipartFile file, @RequestParam("key") String key, @RequestParam(required = true, value = "targetNamespace") String targetNamespace) {
    if (file.getSize() > maxModelSize) {
        throw new UploadTooLargeException("model", maxModelSize);
    }
    LOGGER.info(String.format("uploadModel: [%s]", file.getOriginalFilename()));
    try {
        IModelImporter importer = importerService.getImporterByKey(key).get();
        UploadModelResult result = importer.upload(FileUpload.create(file.getOriginalFilename(), file.getBytes()), Context.create(getUserContext(targetNamespace), Optional.of(targetNamespace)));
        if (!result.isValid()) {
            result.setMessage(String.format(UPLOAD_FAIL, file.getOriginalFilename()));
        } else {
            if (result.hasWarnings()) {
                result.setMessage(String.format(UPLOAD_WARNING, file.getOriginalFilename()));
            } else {
                result.setMessage(String.format(UPLOAD_VALID, file.getOriginalFilename()));
            }
        }
        return new ResponseEntity<>(result, HttpStatus.OK);
    } catch (IOException e) {
        return new ResponseEntity<>(new UploadModelResult(null, e.getMessage(), Collections.emptyList()), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Also used : UploadModelResult(org.eclipse.vorto.repository.importer.UploadModelResult) ResponseEntity(org.springframework.http.ResponseEntity) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) IModelImporter(org.eclipse.vorto.repository.importer.IModelImporter) IOException(java.io.IOException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UploadModelResult

use of org.eclipse.vorto.repository.importer.UploadModelResult in project vorto by eclipse.

the class MappingTest method tesUploadMapping.

@Test
public void tesUploadMapping() throws IOException {
    UploadModelResult uploadResult = this.importer.upload(FileUpload.create("Color.type", IOUtils.toByteArray(new ClassPathResource("sample_models/Color.type").getInputStream())), Context.create(createUserContext("admin", "playground"), Optional.empty()));
    assertEquals(true, uploadResult.isValid());
    assertNotNull(uploadResult.getHandleId());
    ModelInfo resource = uploadResult.getReports().get(0).getModel();
    assertEquals("org.eclipse.vorto.examples.type", resource.getId().getNamespace());
    assertEquals("Color", resource.getId().getName());
    assertEquals("1.0.0", resource.getId().getVersion());
    assertEquals(ModelType.Datatype, resource.getType());
    assertEquals(0, resource.getReferences().size());
    assertEquals("Color", resource.getDisplayName());
    assertNull(resource.getDescription());
    assertEquals(0, repositoryFactory.getRepository(createUserContext("admin", "playground")).search("*").size());
}
Also used : UploadModelResult(org.eclipse.vorto.repository.importer.UploadModelResult) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 3 with UploadModelResult

use of org.eclipse.vorto.repository.importer.UploadModelResult in project vorto by eclipse.

the class MappingTest method testCheckinValidMapping.

@Test
public void testCheckinValidMapping() throws Exception {
    IUserContext userContext = createUserContext("admin", "playground");
    UploadModelResult uploadResult = this.importer.upload(FileUpload.create("Color.type", IOUtils.toByteArray(new ClassPathResource("sample_models/Color.type").getInputStream())), Context.create(userContext, Optional.empty()));
    assertEquals(true, uploadResult.isValid());
    assertEquals(0, repositoryFactory.getRepository(userContext).search("*").size());
    User user = new User();
    user.setUsername("alex");
    Collection<User> users = new ArrayList<>();
    users.add(user);
    when(userRepository.findAll()).thenReturn(users);
    this.importer.doImport(uploadResult.getHandleId(), Context.create(createUserContext("alex", "playground"), Optional.empty()));
    assertEquals(1, repositoryFactory.getRepository(userContext).search("*").size());
    uploadResult = this.importer.upload(FileUpload.create("sample.mapping", IOUtils.toByteArray(new ClassPathResource("sample_models/sample.mapping").getInputStream())), Context.create(createUserContext("admin", "playground"), Optional.empty()));
    assertEquals(true, uploadResult.getReports().get(0).isValid());
    this.importer.doImport(uploadResult.getHandleId(), Context.create(createUserContext("alex", "playground"), Optional.empty()));
    assertEquals(1, repositoryFactory.getRepository(userContext).search("type:Mapping").size());
}
Also used : UploadModelResult(org.eclipse.vorto.repository.importer.UploadModelResult) User(org.eclipse.vorto.repository.domain.User) ArrayList(java.util.ArrayList) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

UploadModelResult (org.eclipse.vorto.repository.importer.UploadModelResult)3 Test (org.junit.Test)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 User (org.eclipse.vorto.repository.domain.User)1 IModelImporter (org.eclipse.vorto.repository.importer.IModelImporter)1 UploadTooLargeException (org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException)1 ResponseEntity (org.springframework.http.ResponseEntity)1 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1