use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.
the class GeneratedOutputAttachmentHandlerTest method attachGeneratedOutput.
@Test
public void attachGeneratedOutput() {
// setup
String filename = "thiswasgenerated.json";
byte[] content = { 0x01 };
List<Attachment> attList = new ArrayList<>();
ModelId id = new ModelId();
id.setName("TestInfoModel");
attList.add(Attachment.newInstance(id, filename));
when(repository.getAttachmentsByTags(any(), any())).thenReturn(attList);
ModelInfo modelInfo = new ModelInfo();
modelInfo.setId(id);
Map<String, String> requestParams = new HashMap<>();
GeneratorPluginConfiguration plugin = GeneratorPluginConfiguration.of("eclipseditto", "v2", "http://localhost:8888", "1.0.0");
when(repository.getAttachmentContent(any(), any())).thenReturn(Optional.of(new FileContent(filename, content)));
when(modelRepositoryFactory.getRepositoryByModel(any(), any())).thenReturn(repository);
IUserContext userContext = new IUserContext() {
@Override
public Authentication getAuthentication() {
return null;
}
@Override
public String getUsername() {
return "testuser";
}
@Override
public String getWorkspaceId() {
return null;
}
@Override
public String getHashedUsername() {
return null;
}
@Override
public boolean isAnonymous() {
return false;
}
@Override
public boolean isSysAdmin() {
return false;
}
};
GeneratedOutput generatedOutput = sut.getGeneratedOutputFromAttachment(modelInfo, requestParams, plugin, repository).get();
// execute
GeneratedOutput result = sut.attachGeneratedOutput(userContext, modelInfo.getId(), "eclipseditto", requestParams, generatedOutput, plugin);
// verify
assertNotNull(result);
assertEquals(1, result.getSize());
assertEquals("generated_eclipseditto_1.0.0_TestInfoModel.json", result.getFileName());
assertEquals(content, result.getContent());
}
use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.
the class GeneratedOutputAttachmentHandlerTest method getGeneratedOutputFromAttachment.
@Test
public void getGeneratedOutputFromAttachment() {
// setup
String filename = "thiswasgenerated.json";
byte[] content = { 0x01 };
List<Attachment> attList = new ArrayList<>();
ModelId id = new ModelId();
id.setName("TestInfoModel");
attList.add(Attachment.newInstance(id, filename));
when(repository.getAttachmentsByTags(any(), any())).thenReturn(attList);
ModelInfo modelInfo = new ModelInfo();
modelInfo.setId(id);
Map<String, String> requestParams = new HashMap<>();
GeneratorPluginConfiguration plugin = GeneratorPluginConfiguration.of("eclipseditto", "v2", "http://localhost:8888", "1.0.0");
when(repository.getAttachmentContent(any(), any())).thenReturn(Optional.of(new FileContent(filename, content)));
// execute
Optional<GeneratedOutput> result = sut.getGeneratedOutputFromAttachment(modelInfo, requestParams, plugin, repository);
// verify
assertTrue(result.isPresent());
assertEquals(content, result.get().getContent());
assertEquals(filename, result.get().getFileName());
assertEquals(1, result.get().getSize());
}
use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.
the class GenericGeneratorController method generateAndExtract.
@RequestMapping(value = "/{serviceKey}/models/{modelId:.+}/!/**", method = RequestMethod.GET)
@CrossOrigin(origins = "https://www.eclipse.org")
public void generateAndExtract(@ApiParam(value = "The iD of vorto model, e.g. com.mycompany:Car:1.0.0", required = true) @PathVariable final String modelId, @ApiParam(value = "Service key for a specified platform, e.g. lwm2m", required = true) @PathVariable String serviceKey, final HttpServletRequest request, final HttpServletResponse response) {
try {
ModelId modelIdToGen = ModelId.fromPrettyFormat(modelId);
GeneratedOutput generatedOutput = generatorService.generate(getUserContext(modelIdToGen), modelIdToGen, URLDecoder.decode(serviceKey, "utf-8"), getRequestParams(request));
String extractPath = getExtractPath(request);
if (extractPath == null || extractPath.trim().isEmpty()) {
writeToResponse(response, generatedOutput);
return;
}
if (generatedOutput.getFileName().endsWith(ZIPFILE_EXTENSION)) {
Optional<GeneratedOutput> extractionResult = extractFromZip(generatedOutput.getContent(), extractPath);
if (extractionResult.isPresent()) {
writeToResponse(response, extractionResult.get());
return;
}
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (IOException e) {
throw new RuntimeException("Error copying file.", e);
}
}
use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.
the class GenericGeneratorController method extractFromZip.
private Optional<GeneratedOutput> extractFromZip(byte[] zipFile, String filenameInZip) throws IOException {
Objects.requireNonNull(zipFile);
Objects.requireNonNull(filenameInZip);
ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(zipFile));
ZipEntry ze = null;
while ((ze = zipInputStream.getNextEntry()) != null) {
if (ze.getName().equals(filenameInZip)) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(9000);
byte[] buffer = new byte[9000];
int len = 0;
while ((len = zipInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
byte[] outputBytes = outputStream.toByteArray();
return Optional.of(new GeneratedOutput(outputBytes, Paths.get(ze.getName()).getFileName().toString(), outputBytes.length));
}
}
return Optional.empty();
}
use of org.eclipse.vorto.repository.plugin.generator.GeneratedOutput in project vorto by eclipse.
the class AbstractGeneratorController method generateAndWriteToOutputStream.
protected void generateAndWriteToOutputStream(String modelId, String pluginKey, Map<String, String> params, HttpServletResponse response) {
ModelId modelIdToGen = ModelId.fromPrettyFormat(modelId);
try {
GeneratedOutput generatedOutput = generatorService.generate(getUserContext(modelIdToGen), modelIdToGen, URLDecoder.decode(pluginKey, "utf-8"), params);
writeToResponse(response, generatedOutput);
} catch (IOException e) {
throw new RuntimeException("Error copying file.", e);
}
}
Aggregations