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);
}
}
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());
}
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());
}
Aggregations